获取对象数组中的所有参数

时间:2016-03-23 14:17:17

标签: javascript arrays

我有以下JavaScript JSON数组的房地产家庭对象

var json = {
    'homes': [{
    "home_name": "tiger",
            "home_id": "1",
            "price": "925",
            "sqft": "1100",
            "num_of_beds": "2",
            "num_of_baths": "2.0",
        }, {
    "home_name": "lion",
            "hom`enter code here`e_id": "2",
            "price": "1425",
            "sqft": "1900",
            "num_of_beds": "4",
            "num_of_baths": "2.5",
        },  
    {
    "home_name": "kingkong",
            "home_id": "3",
            "price": "1425",
            "sqft": "1900",
            "num_of_beds": "4",
            "num_of_baths": "2.5",
        },


    ]
}

我想要的是显示一个看起来像这样的新数组

console.log(newarray);

// [老虎,狮子,金刚]

希望有人可以帮助我

1 个答案:

答案 0 :(得分:1)

试试这个



var json = {
    'homes': [{
    "home_name": "tiger",
            "home_id": "1",
            "price": "925",
            "sqft": "1100",
            "num_of_beds": "2",
            "num_of_baths": "2.0",
        }, {
    "home_name": "lion",
            "home_id": "2",
            "price": "1425",
            "sqft": "1900",
            "num_of_beds": "4",
            "num_of_baths": "2.5",
        },  
    {
    "home_name": "kingkong",
            "home_id": "3",
            "price": "1425",
            "sqft": "1900",
            "num_of_beds": "4",
            "num_of_baths": "2.5",
        },


    ]
}

var newarray = json.homes.map(function(value){return value.home_name});
              
document.body.innerHTML = JSON.stringify(newarray, 0,4)