3

Suppose I have the following array:

let array = [
  {
    id: "1",
    name: "name",
    categories: [
      {
        subid: "10",
        name: "name",
      },
      {
        subid: "11",
        name: "name",
      }
    ]
  },
  {
    id: "2",
    name: "name",
    categories: [
      {
        subid: "20",
        name: "name",
      },
      {
        subid: "21",
        name: "name",
      }
    ]
  }
]

My goal is to take the id of each of the objects and add it to the inner array categories. So it would look like this:

let array = [
  {
    id: "1",
    name: "name",
    categories: [
      {
        subid: "10",
        name: "name",
        id: "1"
      },
      {
        subid: "11",
        name: "name",
        id: "1"
      }
    ]
  },
  {
    id: "2",
    name: "name",
    categories: [
      {
        subid: "20",
        name: "name",
        id: "2"
      },
      {
        subid: "21",
        name: "name",
        id: "2"
      }
    ]
  }
]

Here is what I have so far:

array.map(x => (x.id)) // returns new array of ids
// add these into the categories

How can I do this using map? If map can't be used I think for each will work as well.

2
  • do you want to mutate the original data or get independent new objects? Commented Sep 28, 2018 at 8:04
  • I want to get a new array, from the original data Commented Sep 28, 2018 at 8:09

5 Answers 5

2

With map method and spread syntax inside object you could do this.

let array = [{"id":"1","name":"name","categories":[{"subid":"10","name":"name"},{"subid":"11","name":"name"}]},{"id":"2","name":"name","categories":[{"subid":"20","name":"name"},{"subid":"21","name":"name"}]}]

let result = array.map(({id, categories, ...rest}) => ({
  ...rest, id, categories: categories.map((o) => ({...o, id}))
}))

console.log(result)

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

Comments

2

You can use Array.forEach() to iterate over the array and then use Array.map() on categories array to add the id prop to all its objects:

let array = [ { id: "1", name: "name", categories: [ { subid: "10", name: "name", }, { subid: "11", name: "name", } ] }, { id: "2", name: "name", categories: [ { subid: "20", name: "name", }, { subid: "21", name: "name", } ] } ];
array.forEach((o)=>{
  o.categories = o.categories.map(cat=>Object.assign({},cat,{id : o.id}));
});
console.log(array);

2 Comments

that is a much cleaner approach than the other (Nenad's) answer. But OP just commented that he wants a new object from the data. Maybe you can adapt it accordingly.
@KarelG Thank you :-)
2

What about nested map?

let arr = [
  {
    id: "1",
    name: "name",
    categories: [
      {
        subid: "10",
        name: "name",
      },
      {
        subid: "11",
        name: "name",
      }
    ]
  },
  {
    id: "2",
    name: "name",
    categories: [
      {
        subid: "20",
        name: "name",
      },
      {
        subid: "21",
        name: "name",
      }
    ]
  }
]

arr.map(x=>{
    x.categories.map(y => { 
        y.id = x.id
    })
})

console.log(arr)

1 Comment

This will modify the original Array.
1

You need to map all arrays with copied properties to get a new independent data with a new property.

let array = [{ id: "1", name: "name", categories: [{ subid: "10", name: "name", }, { subid: "11", name: "name", }] }, { id: "2", name: "name", categories: [{ subid: "20", name: "name", }, { subid: "21", name: "name", }] }],
    updated = array.map(o => Object.assign(
        {},
        o,
        { categories: o.categories.map(p => Object.assign({}, p, { id: o.id })) }
    ));

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

1 Comment

yes! this is what im looking for. thank you so much.
1

Using each of Jquery should do the job. $.each(array,function(index,item){item.categories.id = item.id;}); Thanks.

Comments

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.