首页 > 解决方案 > 如果所有数组的长度不同,如何将嵌套数组中的所有信息查询到dom?

问题描述

即使数组的长度不同,我如何输出特征类别?我已经能够使用 projects.features[1] 输出代码,但由于嵌套数组长度的大小不同,我不确定该怎么做。

const peet = document.querySelector('.projectInserts');

const projects = [
  {"Title":"InstaJam",
   "image":"img/ig.jpg",
   "Gif":"gif-title",
   "Github":"github",
   "description":["first","second", "third"],
   "features":["PHP / Laravel", "Html and CSS", "Blade", "Composer", 
    "User authentication", "MySQL" 
    ],
   "Link":"link",
   "class": "app"
 },
  {"Title":"Kayak HTML Email",
   "image":"img/kayak.png",
   "Gif":"gif-title",
   "Github":"github",
   "description":["first","second", "third"],
   "features":[ 
    "User authentication", "MySQL" 
    ],
   "Link":"link"
 },

标签: javascriptarraysnested

解决方案


不太确定您想要什么,但这会将您的所有功能集中在一个阵列中。

let getIndividualFeatures = []

for(let i = 0; i < projects.length; i++){
    getIndividualFeatures.push(projects[i].features)
}

let allFeatures = [].concat.apply([], getIndividualFeatures);

console.log(allFeatures)

编辑:如果您想让它们分开,请检查console.log(getIndividualFeatures)之前allFeatures是否已启动。


推荐阅读