1

I have an example code below :

z = [] //To get all the Id    
x = [
        {
           _id: "12345",
           name: "A"      
        },
        {
           _id: "67890",
           name: "B"
        }
    ]

The question is how I can get all the "_id" from "x" and save it to "z". So the results like this:

z = ["12345", "67890"]

Any answer would be appreciated

2 Answers 2

1

Use array.map

var  x = [
        {
           "_id": "12345",
           "name": "A"      
        },
        {
           "_id": "67890",
           "name": "B"
        }
    ];
var result = x.map(a => a._id);
console.log(result);

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

Comments

1

This is a very simple Array#map()

   
const x = [{
    _id: "12345",
    name: "A"
  },
  {
    _id: "67890",
    name: "B"
  }
]

const z = x.map(o => o._id);

console.log(z)

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.