循环遍历对象并获取关键帧的所有值



我正试图循环遍历一个对象,并获取所有作为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" }
],
}
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
}
}

如果我这样做,它将返回undefined。有什么方法可以将这些条件拟合到一个for循环中并获得所需的结果吗?

这将完成任务。

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

输出:

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

说明:

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

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

这转化为:(condition==true(?(如果为true,则执行此操作(:(如果为false,则执行该操作(

如果数组不存在,则条件将不满足。因此;(如果为false,则执行此操作(";部分将被执行。

然后,我们对第二个数组执行同样的操作。通过使用";concat";,我们将两个数组连接成一个数组。然后,我们使用";地图";将整个对象变换为"0"的值;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);
});

对于更多的数组,只需将它们添加到用逗号分隔的.concat()函数中:

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)
}

您可以将想要的对象连接到一个数组中,并映射出您想要访问的任何值。这就是你要找的吗?

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);
});

最新更新