How to select a particular field from javascript array

前端 未结 4 1609
小蘑菇
小蘑菇 2021-01-04 15:48

I have an array object in javascript. I would to select a particular field from all the rows of the object.

I have an object like

var sample = {
[Nam         


        
4条回答
  •  Happy的楠姐
    2021-01-04 15:57

    That Javascript has no meaning. It is syntatically incorrect. I assume you meant:

    var sample = [
        {Name:"a",Age:1},
        {Name:"b",Age:2},
        {Name:"c",Age:3}
    ]
    

    Then you can use jQuery to do something like this:

    var names = $(sample).map(function(){ return this.Name; });
    

    But in reality all jQuery is doing is looping through the object for you. Writing your own loop would be (insignificantly) faster. There is no way to do this without a loop.

提交回复
热议问题