繁体   English   中英

循环一个 object 并获取所有键的值

[英]Loop through an object and get all the values of the keys

我正在尝试遍历 object 并获取名称的所有键值,我正在通过以下方式进行操作:

var fakeData = {
     "manufacturer": "tesla",
     "cars": [
          {"title": "CALI", "name": "CALI", "type": "string" },
          {"title": "TEXAS", "name": "TEXAS", "type": "string" },
          {"title": "NY", "name": "NY", "type": "string" }
     ],
     "usedCars": [
          {"title": "FL", "name": "FL", "type": "string" }
     ],
}

returnName(fakeData) {
    for (key in fakeData.cars && fakeData.usedCars) {
     //return name property  of cars and usedCars arrays.
     //fakeData is the representation of the req.body data that might 
     //sometimes not necessarily have either cars or usedCars

    }

}

如果我这样做,它会返回未定义。 有没有办法在一个 for 循环中适应这些条件并获得所需的结果?

这将完成这项工作。

(fakeData.cars?fakeData.cars:[]).concat(fakeData.usedCars?fakeData.usedCars:[]).map(car => car.name)

Output:

["CALI", "TEXAS", "NY", "FL"]

解释:

首先,我们使用条件来检查 faekData.cars 是否真的存在。 如果是,请获取数组。 如果不是,则在其位置返回一个空数组。

  (fakeData.cars)?fakeData.cars:[]

这转化为:(条件==真)? (如果为真,请执行此操作):(如果为假,请执行此操作)

如果数组不存在,则不满足条件。 因此,“(如果为假,则执行此操作)”部分将被执行。

然后,我们对第二个数组做同样的事情。 通过使用“concat”,我们将两个 arrays 连接成一个。 然后,我们使用“map”将整个对象转换为“name”属性的值。

希望这就是你要找的。

 var fakeData = { "manufacturer": "tesla", "cars": [ {"title": "CALI", "name": "CALI", "type": "string" }, {"title": "TEXAS", "name": "TEXAS", "type": "string" }, {"title": "NY", "name": "NY", "type": "string" } ], "usedCars": [ {"title": "FL", "name": "FL", "type": "string" } ], }; [].concat( fakeData.cars? fakeData.cars: [], fakeData.usedCars? fakeData.usedCars: [], fakeData.undefinedCars? fakeData.undefinedCars: [], ).forEach(car => { console.log(car.name); });

更多 arrays 只需将它们添加到.concat() function 中,用逗号分隔:

array1.concat(array2, array3, array4)

for (key in fakeData.cars && fakeData.usedCars)并不意味着“给我汽车和二手车的钥匙”,它的意思是“给我汽车和二手车评估的钥匙”(这是汽车,因为它是真实的) .

相反,只需使用.map并获取名称:

 var fakeData = { "manufacturer": "tesla", "cars": [ {"title": "CALI", "name": "CALI", "type": "string" }, {"title": "TEXAS", "name": "TEXAS", "type": "string" }, {"title": "NY", "name": "NY", "type": "string" } ], "usedCars": [ {"title": "FL", "name": "FL", "type": "string" } ], } /* This doesn't mean "get me the keys in cars and usedCars" it means "get me the keys of the evaluation of cars && usedCars" (which is cars since it's truthy) returnName(fakeData) { for (key in fakeData.cars && fakeData.usedCars) { //return name property of cars and usedCars arrays. //fakeData is the representation of the req.body data that might //sometimes not necessarily have either cars or usedCars } } */ // You can just map the arrays and spread them into a new array: // If you have to deal with certain properties not being there, // you can use optional chaining, the? and?? [] pieces const names = [...fakeData?.cars?.map(({name}) => name)?? [], ...fakeData?.usedCars?.map(({name}) => name)?? [] ]; console.log(names); // ["CALI", "TEXAS", "NY", "FL"]

这应该打印所有的汽车名称

var fakeData = {
     "manufacturer": "tesla",
     "cars": [
          {"title": "CALI", "name": "CALI", "type": "string" },
          {"title": "TEXAS", "name": "TEXAS", "type": "string" },
          {"title": "NY", "name": "NY", "type": "string" }
     ],
     "usedCars": [
          {"title": "FL", "name": "FL", "type": "string" }
     ],
}

var results = [];

function returnNames(){
    for(index in fakeData.cars){
        results.push(fakeData.cars[index].title);
    }
    for(index in fakeData.usedCars){
        results.push(fakeData.usedCars[index].title);
    }
    
    console.log("car names: " + results)
}

您可以将您想要的对象连接到一个数组中,然后将 map 输出您想要访问的任何值。 那是你要找的吗?

 const fakeData = { manufacturer: 'tesla', cars: [ { title: 'CALI', name: 'CALI', type: 'string' }, { title: 'TEXAS', name: 'TEXAS', type: 'string' }, { title: 'NY', name: 'NY', type: 'string' } ], usedCars: [{ title: 'FL', name: 'FL', type: 'string' }] }; const combinedCarData = [...fakeData.cars, ...fakeData.usedCars]; // Map whatever values you would like to access combinedCarData.map(car => { console.log(car.title); console.log(car.name); console.log(car.type); });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM