-3

I have 2 Javascripts Array

var array1 = ["Mergen Morry","Dash Borad","Mergen Xss"];
var array2 = ["02111356","4458763","02111356"];

I want the output to be like this

Mergen Morry – 02111356 : data uploaded
Dash Borad - 4458763 : data uploaded
Mergen Xss – 02111356 : id already registered

I was thinking using join(), but as I recall this is 2 array, and using concat() are out of question, and maybe was thinking using map() or forEach(), but I still don't get how.

Edit : I'm sorry, the output should be string not array.

drfrost
  • 5
  • 1
  • 2

2 Answers2

1

You could take an object for keeping seen id and return appropriate comments for mapping strings.

const
    names = ["Mergen Morry", "Dash Borad", "Mergen Xss"],
    ids = ["02111356", "4458763", "02111356"],
    result = names.map(
        (seen => (s, i) => `${s} - ${ids[i]} : ${ seen[ids[i]]
            ? 'id already registered'
            : (seen[ids[i]] = true, 'data uploaded') }`)
        ({})
    ).join('\n');

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • It should show list of string nina, appreciate your answer, the question was asking me to show a list based on it. – drfrost Nov 28 '20 at 01:39
0

@Nina Scholz answer is pretty good, and here the another way you could have json with all its content instead.

var array1 = ["Mergen Morry","Dash Borad","Mergen Xss"];
var array2 = ["02111356","4458763","02111356"];

var res = array1.map((item,index)=>{
 return {name:item,id:array2[index]}
},[]);
// Now that you have an array with Id and namne, you could loop throw each array and display the data


console.log(res)
Alen.Toma
  • 4,684
  • 2
  • 14
  • 31