0

Well, basically I have an array that has an objects with names and this objects has an array of objects inside, looks likes this:

var array = [
  {articles: [
    {number: "123"},
    {number: "143"},
  ]},
  {paragraph: [
    {number: "197"},
  ]},
]

And I'm really willing to get a object value in return, like this

{articles: [...], paragraph: [...]}

Can someone help me, please?

1
  • Please add the code you've attempted to your question as a minimal reproducible example. Commented Jan 26, 2022 at 21:20

4 Answers 4

1

This can be done using an array reducer.

const array = [
  { articles: [{ number: "123" }, { number: "143" }] },
  { paragraph: [{ number: "197" }] },
];

const formatted = array.reduce((accumulator, currentValue) => {
    const [[key, value]] = Object.entries(currentValue);
    return { ...accumulator, [key]: value }
}, {});

console.log(formatted);

What we're doing is initializing our reducer with an empty object on line 9, and iterating over the array. Each time we iterate, we're returning the object with the new key and value appended to the end of it.

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

1 Comment

Thank you, this solved the problem with no worries <3
1

You could group by the outer properties of the nested objects.

const
    array = [{ articles: [{ number: "123" }, { number: "143" }] }, { paragraph: [{ number: "197" }] }],
    result = array.reduce((r, o) => {
        Object
            .entries(o)
            .forEach(([k, a]) => (r[k] ??= []).push(...a));

        return r;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

1 Comment

Thanks, I used the first answer, both are equivalence, and I only took the first one because as named, was the first, so I used, but thank you, I upvoted your solution too <3
1

Iterate over the array, and use Object.entries to get the keys and values.

const array=[{articles:[{number:"123"},{number:"143"}]},{paragraph:[{number:"197"}]}];

const out = {};

for (const obj of array) {
  const [[key, value]] = Object.entries(obj);
  out[key] = value;
}

console.log(out);

1 Comment

Thanks, I used the first answer, using the reduce version, but your version was nice too, and very intuitive, thank you again <3
0
let array = [
  {articles: [
    {number: "123"},
    {number: "143"},
  ]},
  {paragraph: [
    {number: "197"},
  ]},
]


let obj = {}
array.forEach(a => {

   if('articles' in a){
       obj.articles = a.articles
   }

   if('paragraph' in a){
       obj.paragraph = a.paragraph
   }
})

console.log(obj)

1 Comment

Worked, but, this solution requires to add a condition for every new type imputed, so, not that scalable, although I found this a good answer, if I've only two types, but thank you anyway around.

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.