0

I am trying to flatten an array of objects. The only real informations i require is the arrays compacted into a single array.

The content is as follows:

const content = [
    { "chocolate" : [1] },
    { "banana"    : [5] },
    { "soap"      : [2] },
    { "tea"       : [4] },
];

All i am interested in is the values in the array. So desired result would be:

const result = [1,5,2,4]

I have tried

Object.keys(content).map((val) =>  Object.values(content[val]));

and even tried creating a function

const flatten = ({ children = [], ...rest }) => [rest, ...children.flatMap(flatten)];

and calling it like so:

 console.log(flatten(content));

but no luck. Help?

3
  • 1
    Use flatMap. That is the modern way Commented Jan 27, 2023 at 17:08
  • content.flatMap(Object.values).flat() or content.map(Object.values).flat(2) Commented Jan 27, 2023 at 17:17
  • content.flatMap(obj => Object.values(obj)[0]) will do the work. Commented Jan 27, 2023 at 17:19

5 Answers 5

1

The easiest way is to flat-map the entries, and pop the first value off of the object.

const
  content  = [
    { "chocolate" : [1] },
    { "banana"    : [5] },
    { "soap"      : [2] },
    { "tea"       : [4] },
  ],
  numbers = content.flatMap((obj) => Object.values(obj).pop());

console.log(JSON.stringify(numbers)); // [1, 5, 2, 4]

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

Comments

0

// source variable
const content = [{
  "chocolate": [1]
}, {
  "banana": [5]
}, {
  "soap": [2]
}, {
  "tea": [4]
}]
// to store result value
const result = [];
// lopoping though each object and accesing values of pbject using **Object.values(obj)** an array indexes.
content.forEach(val => result.push(Object.values(val)[0][0]));
// Printing result
console.log(result);

Comments

0
const result = content.map(o => Object.values(o).shift()[0])

2 Comments

Your answer could be improved by adding more information on what the code does and how it helps the OP.
@Tyler2P, Possibly if I explain in words each part of the code it will be easier to understand what it does. But to what level do I explain, what "const" means, what the "map" function does, what the "Object values" method does. What does the function "shift() of Array" do, what does the subscript [0] of an Array do? If you don't understand some of the code, take a part of it and do some research on Google, stackoverflow.com, ChatGpt, etc. Here nobody is paid to try to help others, and in my opinion everyone helps as they want.
0

You should use flatMap with Object.values:

const content  = [{"chocolate": [1]}, {"banana": [5]},{"soap": [2]},{"tea": [4]}]
const res = content.flatMap(e => Object.values(e)[0]);
console.log(res);

Comments

0

As easy as this content.flatMap(obj => Object.values(obj)[0])

1 Comment

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.

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.