How to select a particular field from javascript array

前端 未结 4 1530
小蘑菇
小蘑菇 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条回答
  •  鱼传尺愫
    2021-01-04 16:06

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

    Well, your biggest problem there is that no matter what happens, you'll have to loop. Now, jQuery will let you hide that behavior with:

    var output = []
    $.each( sample, function(i,e){ output.push( e.Name )} );
    

    But fundamentally, that is the same thing as:

    for( var i = 0; i < sample.length; i++ )
    {
       output.push( v.Name )
    }
    

提交回复
热议问题