Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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
Use map
myArrayResult = myArray.map(function (el) { return el.id; // if you want get id as Number, just add +el.id })
Example
Add a comment
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); }
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>
Required, but never shown
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.
Explore related questions
See similar questions with these tags.