简体   繁体   English

循环通过 object 并返回特定属性的值

[英]Loop through an object and return the values of a specific property

I am trying to loop through an object and get the specific properties values, but I am only either getting the keys or the values.我正在尝试遍历 object 并获取特定的属性值,但我只能获取键或值。 Here is what I am doing:这是我正在做的事情:

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" }
     ],
}

for (key in fakeData) {
 console.log(`${key}:${fakeData[key]}`)
}

I am trying to get the title of the cars property.我正在尝试获取汽车财产的所有权。 I have tried doing ${key.cars}:${fakeData[key.cars]} but I get undefined.我试过做${key.cars}:${fakeData[key.cars]}但我没有定义。 Any suggestions how to access that property?任何建议如何访问该属性? TIA TIA

Do you mean something like this?你的意思是这样的吗?

 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" }, ], } // using map let carTitles = fakeData.cars.map(({title})=>title); console.log(carTitles); // using for loop let carTitles2=[]; for ({title} of fakeData.cars) carTitles2.push(title); console.log(carTitles2); console.log('cars as string are:', carTitles2.join(',')); // both cars and usedCars using one loop let cs = ''; let ucs = ''; let clen=fakeData.cars.length; let uclen=fakeData.usedCars.length; let len=Math.max(clen,uclen); for (let i=0;i<len;i++) { if (clen>0 && i<clen) cs = cs + (i? ',': '') + fakeData.cars[i].title; if (uclen>0 && i<uclen) ucs = ucs + (i? ',': '') + fakeData.usedCars[i].title; } console.log('cars are:', cs); console.log('used cars are:', ucs);

This will loop through cars object and logs the car titles.这将遍历汽车 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" } ], } let result = '' for (car of fakeData.cars) { result += `The new cars are in ${car.title}\n`; } console.log(result);

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

相关问题 如何循环并返回javascript对象的值? - How to loop through and return the values of a javascript object? 循环遍历嵌套数组以返回对象的值 - Loop through nested array to return values of object 如何循环遍历具有特定条件的对象并返回其值的数组? - How can I loop through an object with a specific condition and return an array of its values? Javascript - 当对象的属性值与选项数组中的所有值匹配时,遍历一组选项并返回一个对象? - Javascript - Loop through an array of options and return an object when the object's property values match all values in the options array? 遍历对象并返回所有属性数组的值 - loop through object and return value of all property arrays 如何遍历并从复杂对象(数组)中提取属性值? - How to loop through and extract property values from a complex object (Array)? Javascript循环遍历数组并返回结束连续值的对象 - Javascript loop through an array and return the object(s) that end a streak of values 使用函数在对象中循环以更改值,然后返回结果 - Using function to for-in loop through object to change values then return result 循环通过 object 并仅返回某些键及其值 - Loop through an object and only return certain keys together with their values 返回具有特定属性的数组中的对象 - Return object in array with specific property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM