I have a an array of objects, and when I am looping over it add adding a value to a variable, the variable always has an nullin it. I know its a pretty basic question, but stuck on why it adds the null and don't know how to stop it.
(function() {
let ds = [{
Name: "A",
Age: 1
},
{
Name: "B",
Age: 2
},
{
Name: "C",
Age: 3
}
];
let str = null;
for (let i = 0; i < ds.length; i++) {
if (ds[i].Name) {
str += ds[i].Name + ",";
}
}
console.log(str);
})();
It shows as null, A, B, C, and what I want it to show as A,B,C
null + "42". This should answer your question. Use an empty string initially, or better yet,ds.map(e => e.Name).join(",")since everything has a name ords.filter(e => e.Name !== undefined).map(e => e.Name).join(",")if you do have undefNamekeys.