Home > Enterprise >  Unable to flatten an array of objects into a single array
Unable to flatten an array of objects into a single array

Time:01-28

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?

CodePudding user response:

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]

CodePudding user response:

// 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);

CodePudding user response:

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

CodePudding user response:

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);

CodePudding user response:

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

CodePudding user response:

There are several ways to flatten an array of objects into a single array in Python. One way is to use the itertools.chain method from the itertools module along with a list comprehension. For example:

import itertools

objects = [
    {'a': [1, 2, 3]},
    {'b': [4, 5, 6]},
    {'c': [7, 8, 9]}
]

flattened_list = list(itertools.chain(*[obj.values() for obj in objects]))

print(flattened_list)

This will output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Another way to flatten an array of objects is to use the reduce() function from the functools module along with a lambda function. For example:

import functools

objects = [
    {'a': [1, 2, 3]},
    {'b': [4, 5, 6]},
    {'c': [7, 8, 9]}
]

flattened_list = functools.reduce(lambda acc, obj: acc   obj.values(), objects, [])

print(flattened_list)

This will also output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

In case you want to flatten the array of nested lists, you can use the itertools.chain method from the itertools module along with the * operator. For example:

import itertools

objects = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

flattened_list = list(itertools.chain(*objects))

print(flattened_list)

This will output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

It's important to note that the approach you use will depend on the structure of the objects in the array and what you want to achieve with the flattened list.

  • Related