1

So I have :

list = 
    {
      id: 1,
      arr: [
        {index : 1 , description: "lol" , author: "Arthur"},
        {index : 2 , description: "sdadsa" , author: "Bob"},
        {index : 3 , description: "loasd" , author: "Mackenzie"}
      ]
    }

and I want to create an array only with description and author property from the arr array.

I tried var a = {l : list.arr.map(x => {x.description,x.author})}. But all items from the array are undefined .

4
  • The arr in your list is not array, are you sure no typo here? Commented Jul 19, 2021 at 6:43
  • Thank you, i edited the question. This is a simplified version of what I'm trying to do. Commented Jul 19, 2021 at 6:45
  • Are you still have problem? @radurbalau Commented Aug 9, 2021 at 12:11
  • No, i figured it out ! Thanks everyone ! Commented Aug 18, 2021 at 14:33

4 Answers 4

4

Another approach would be to use the rest parameter. This way you can remove the index and keep everything else intact.

var list = {
  id: 1,
  arr: [
    { index: 1, description: "lol", author: "Arthur" },
    { index: 2, description: "sdadsa", author: "Bob" },
    { index: 3, description: "loasd", author: "Mackenzie" },
  ],
};

var a = list.arr.map(({index, ...rest}) => rest);

console.log(a);

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

1 Comment

Wow, this was great! Amazing thought!
1

You almost finish, you should define the key in your return object in the map funciton.

var list = {
  id: 1,
  arr: [
    { index: 1, description: "lol", author: "Arthur" },
    { index: 2, description: "sdadsa", author: "Bob" },
    { index: 3, description: "loasd", author: "Mackenzie" },
  ],
};

var a = list.arr.map(x => ({
  description: x.description,
  author: x.author,
}));

console.log(a);

2 Comments

So the mistake was that i didnt had ( ) wrapped to the object when I was returning in map ?
@radurbalau, yes. Also, you should define the key for the object too.
1

list = {
  id: 1,
  arr: [{
      index: 1,
      description: "lol",
      author: "Arthur"
    },
    {
      index: 2,
      description: "sdadsa",
      author: "Bob"
    },
    {
      index: 3,
      description: "loasd",
      author: "Mackenzie"
    }
  ]
}

var a = {
  l: list.arr.map(x => ({
    "description": x.description,
    "author": x.author
  }))
}
console.log(a);

Comments

1

You just forget to define keys in return object.

var list = {
  id: 1,
  arr: [
    { index: 1, description: "lol", author: "Arthur" },
    { index: 2, description: "sdadsa", author: "Bob" },
    { index: 3, description: "loasd", author: "Mackenzie" },
  ],
};

var a = l.arr.map(x => ({
  description: x.description,
  author: x.author,
}));

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.