我正在尝试遍历一个对象并获取名称的所有键值,我正在按以下方式进行操作:
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 循环中并获得所需的结果?
答案 0 :(得分:3)
这将完成工作。
(fakeData.cars?fakeData.cars:[]).concat(fakeData.usedCars?fakeData.usedCars:[]).map(car => car.name)
输出:
["CALI", "TEXAS", "NY", "FL"]
说明:
首先,我们使用条件来检查 faekData.cars 是否确实存在。如果是,获取数组。如果不是,则在其位置返回一个空数组。
(fakeData.cars)?fakeData.cars:[]
这转化为:(条件==真)? (如果为真则执行此操作):(如果为假则执行此操作)
如果数组不存在,则不满足条件。因此,将执行“(如果为假,则执行此操作)”部分。
然后,我们对第二个数组做同样的事情。通过使用“concat”,我们将两个数组连接成一个。然后,我们使用“map”将整个对象转换为“name”属性的值。
答案 1 :(得分:1)
希望这就是您要找的东西。
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)
答案 2 :(得分:0)
for (key in fakeData.cars && fakeData.usedCars)
的意思不是“把汽车和二手车的钥匙给我”,它的意思是“给我汽车评价的钥匙&&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"]
答案 3 :(得分:0)
这应该打印所有汽车名称
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)
}
答案 4 :(得分:0)
您可以将您想要的对象连接到一个数组中,并映射出您想要访问的任何值。这就是你要找的吗?
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);
});