0

i have an array like

myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}, etc.]

How can i retrive an array with only id like?

myArrayResult = [73,45]

with jQuery or javascript. thanks

3 Answers 3

5

Use map

myArrayResult = myArray.map(function (el) {
   return el.id;  // if you want get id as Number, just add +el.id
})

Example

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

Comments

1

You could do

var myArrayResult = myArray.map(function (item) {
    return item.id;
}

Or if you don't have a true browser (ie. IE8 or less)

var myArrayResult = [];

for (var i = 0, l = myArray.length; i < l; i++) {
    myArrayResult.push(myArray[i].id);
}

Comments

0

myArray = [{'id':'73','foo':'bar'},{'id':'45','foo':'bar'}];

myArrayResult=[];
$.each(myArray,function(index,val){
     myArrayResult.push(val.id);
});
alert(myArrayResult);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

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.