[
{
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"
]
}
]发布于 2018-12-03 15:06:07
假设用于存储JSON的变量名为dataSet,您将执行以下操作
for(data of dataSet){
console.log(data.subCategory)
}在这个for循环中,您还可以遍历数组的所有subCategory。
发布于 2018-12-03 15:19:02
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)
})发布于 2018-12-03 15:46:21
如果这只是一个简单的JS问题,那么访问数组/对象中的一些数据...
用于访问数组内元素的:
array[index] // first element start from 0.上面的“元素”可以看作是字符串、数字、对象,甚至是数组中的数组。
访问object内部元素的:
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",...]循环如果数组中有多个对象,则需要使用:
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"]https://stackoverflow.com/questions/53587653
复制相似问题