[
{
name:"Technology",
logoImage:require("../../assets/g837.png"),
subCategory:[
"IT",
"Networks",
"Appliances",
"Industrial Machines",
"Medical technology",
"robotics",
"AI",
"Electronics",
"Explosives",
"Machinery",
"Cryptocurrency",
"Electric Vehicles",
"Biotechnology"
]
},
{
name:"Business",
logoImage:require("../../assets/business.png"),
subCategory:[
"Industries",
"Economics",
"Journalism",
"Labor",
"Law",
"Real estate",
"Entrepreneurship",
"Investment",
"Banking",
"Leadership",
"Advertising",
"Business Strategy",
"Marketing",
"E-commerce"
]
},
{
name:"Entertainment",
logoImage:require("../../assets/entertainment.png"),
subCategory:[
"Comedy",
"Dance",
"Dramas",
"Films",
"Gaming",
"Toys",
"Gambling",
"Comics",
"Social sites"
]
}
]
答案 0 :(得分:0)
假设您用于存储JSON的变量称为{
"MOCType":"Temporary Repair","MOCStatusUpdatedDateTime":"2018-10-31T10:00:00Z","TagNumber":"4850-A-101-A01A,4850-A-101-A03C,4850-A-101-A08,4850-A-101-A10A,4850-A-101-A10B,4850-A-101-A10C,4850-A-101-A11,4850-A-101-A12,6320-T-003C","Id":"CLX000A"
}
,您会做的
{
"4850-A-101-A01A" :{
"Temporary Repair (eMOC)": "OK"
},
"4850-A-101-A03C" :{
"Temporary Repair (eMOC)": "OK"
},
"4850-A-101-A08" :{
"Temporary Repair (eMOC)": "OK"
},
"4850-A-101-A10B" :{
"Temporary Repair (eMOC)": "OK"
}
}
在for循环中,您还可以循环访问数组的所有子类别。
答案 1 :(得分:0)
const myArray = [
{
"subCategory":[
"IT",
"Networks"
]
},
{
"subCategory":[
"IT",
"Networks"
]
}
]
myArray.forEach((eachObject) => {
console.log(eachObject.subCategory)
})
假设您已将该数组存储在变量 myArray
中然后,您可以在forLoop的帮助下轻松地迭代subCategory的值。
myArray.forEach((eachObject) => {
console.log(eachObject.subCategory)
})
答案 2 :(得分:0)
如果这只是一个简单的JS问题,请访问数组/对象中的某些数据。...
要访问数组中的元素:
array[index] // first element start from 0.
上面的“元素”可以视为字符串,数字,对象甚至是数组内部的数组。
要访问对象内部的元素,请执行以下操作:
let obj = {a:100,b:200,c:300}
object.key //obj.a means will get the value of 100
或
object["key"] //obj["b"] means will get the value of 200
或
let x = "c"
object[variable] // obj[x] is equal to obj["c"] means will get the value of 300.
因此,要从对象数组中的数组访问“ subCategory”,只需组合以上策略即可:
var nestedArray = [
{
name:"Technology",
logoImage:require("../../assets/g837.png"),
subCategory:[
"IT",
"Networks",
"Appliances",
"Industrial Machines",
"Medical technology",
"robotics",
"AI",
"Electronics",
"Explosives",
"Machinery",
"Cryptocurrency",
"Electric Vehicles",
"Biotechnology"
]
},
{
name:"Business",
logoImage:require("../../assets/business.png"),
subCategory:[
"Industries",
"Economics",
"Journalism",
"Labor",
"Law",
"Real estate",
"Entrepreneurship",
"Investment",
"Banking",
"Leadership",
"Advertising",
"Business Strategy",
"Marketing",
"E-commerce"
]
},
{
name:"Entertainment",
logoImage:require("../../assets/entertainment.png"),
subCategory:[
"Comedy",
"Dance",
"Dramas",
"Films",
"Gaming",
"Toys",
"Gambling",
"Comics",
"Social sites"
]
}
]
var subCategoryArray = nestedArray[0]["subCategory"] // return ["IT", "Networks",...]
如果阵列中有多个对象,则需要使用for循环:
var subCategoryArray = []
for(var i = 0; i < nestedArray.length; i++){
subCategoryArray= subCategoryArray.concat(nestedArray[i]["subCategory"])
}
console.log(subCategoryArray) //return ["IT", "Networks",...,"Comics","Social sites"]