var structureInfos = [{ name: 'HMDB0006285', identifier: 'Six two eight 5' },
{ name: 'HMDB0006288', identifier: 'Six two double eight'},
{ name: 'HMDB0006293', identifier: 'Six two nine three' },
{ name: 'HMDB0006294', identifier: 'Six two Nine Four' }]
var structureElements = [ 'HMDB0006285', 'HMDB0006293', 'HMDB0006294' ]
我想过滤掉值与数组匹配的JSON对象,并返回一个新的JSON对象。我尝试了下面的方法,但似乎不起作用。
预期输出:
Expected : [
{ name: 'HMDB0006285', identifier: 'Six two eight 5' },
{ name: 'HMDB0006293', identifier: 'Six two nine three' },
{ name: 'HMDB0006294', identifier: 'Six two Nine Four' }
]
下面的代码似乎不起作用:
var newArray = [];
structureInfos.forEach(function(structureInfos) {
for (let i=0; i < structureElements.length; i++){
if(structureElements[i] === structureInfos[i]['name']){
newArray.push(structureInfos[i]);
}
}
});
1条答案
按热度按时间hs1ihplo1#