2

It's pretty simple: Object.entries is supposed to produce and Array of key, value pairs.. As such, I would expected this code to destructure

[{
  id: 1,
  name: "christian"
},{
  id : 2,
  name: "bongiorno"
}].map(Object.entries).forEach(([k,v]) => console.log(`${k}: ${v}`));

into producing:

id:1 name:christian
id:2 name:bongiorno

But it doesn't. I get, instead:

id,1: name,christian
id,2: name,bongiorno

What did I miss?

5
  • because your data is not simple Object, its Object Array. Object Array contains Objects so you need nested loop to traverse data. Commented Jul 11, 2017 at 5:45
  • @WasifKhan There's no JSON in the question... plus there's no such thing as a 'JSON Object' Commented Jul 11, 2017 at 5:46
  • I added .reduce((a,b) => a.concat(b),[]) to flatten it. Looks better. Should I update the question ? provide an answer? Commented Jul 11, 2017 at 5:51
  • @ChristianBongiorno That sounds like an answer, which doesn't belong in the question. Commented Jul 11, 2017 at 5:59
  • I added this follow up Commented Jul 11, 2017 at 6:15

2 Answers 2

3

The output is correct but your definition is slightly off, you're missing an array level (array of arrays).

Object.entries is supposed to produce an array of arrays of key, value pairs.

console.log(
  Object.entries({
    id: 1,
    name: 'test'
  })
)

To achieve what you want, you can just update your log to account for nested arrays:

[{
  id: 1,
  name: "christian"
},{
  id : 2,
  name: "bongiorno"
}]
  .map(Object.entries)
  .forEach(([k, v]) => console.log(
    `${k.join(':')} ${v.join(':')}`
  ));

Or maybe you meant to flatten each array?:

[{
  id: 1,
  name: "christian"
},{
  id : 2,
  name: "bongiorno"
}]
  .map(Object.entries)
  .reduce((arr, curr) => arr.concat(curr), [])
  .forEach(([k,v]) => console.log(`${k}: ${v}`));
  

Sign up to request clarification or add additional context in comments.

1 Comment

That's what I was looking for and I did that. But, I am even more baffled by something else. I guess I will create a new question.
2

Let's try to drop the map and forEach and see what you did with each of the objects:

let [k, v] = Object.entries({
  id: 1,
  name: "christian"
});
console.log(`${k}: ${v}`);

let [k, v] = Object.entries({
  id : 2,
  name: "bongiorno"
});
console.log(`${k}: ${v}`);

Now if we expand the Object.entries call, this becomes

let [k, v] = [
  ["id", 1],
  ["name", "christian"]
];
console.log(`${k}: ${v}`);

let [k, v] = [
  ["id", 2],
  ["name", "bongiorno"]
];
console.log(`${k}: ${v}`);

which quite accurately reflects what you're seeing - k and v are getting assigned arrays.

You will need to nest two loops:

const arr = [{
  id: 1,
  name: "christian"
}, {
  id: 2,
  name: "bongiorno"
}];
for (const obj of arr)
    for (const [k, v] of Object.entries(obj))
        console.log(k+": "+v);

2 Comments

@ChristianBongiorno You would need some kind of flatMap for that, which doesn't exist in JS. But if you replace the forEach by a simple for of loop, that's an easy problem
instead of the for loop I just .reduce((a,b) => a.concat(b),[]) to flatten it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.