Javascript 展平数组并查找相应的值,然后写入新数组

Javascript 展平数组并查找相应的值,然后写入新数组,javascript,ecmascript-6,lodash,Javascript,Ecmascript 6,Lodash,目前,我遍历一组LinkedObject来搜索isMatch。此测试查看数组obj.resource_id中是否存在element.id,如果存在,则将匹配的appointmentObj中的可打印字符串添加到printStr中的数组中 问题是因为value.resource\u uniqueids可能是一个包含多个ID的数组,但我的测试只找到一个 不知何故,我需要匹配value.resource\u uniqueids中的所有ID。-在这种情况下,我可能需要为每个value.resource\u

目前,我遍历一组LinkedObject来搜索isMatch。此测试查看数组obj.resource_id中是否存在element.id,如果存在,则将匹配的appointmentObj中的可打印字符串添加到printStr中的数组中

问题是因为value.resource\u uniqueids可能是一个包含多个ID的数组,但我的测试只找到一个

不知何故,我需要匹配value.resource\u uniqueids中的所有ID。-在这种情况下,我可能需要为每个value.resource\u uniqueid添加一个新的appointmentObj,然后连接每个等效的可打印字符串

我希望这是有道理的。如何为value.resource\u uniqueid的每个匹配添加新的${currentMatch.printable string}

谢谢

_.forEach(appointmentObj, (value,i) => {

    // value.resource_uniqueids is always an array, most of the time it only has one element
    // but sometimes it can have more than one which means we only match one below in isMatch function

    _.set(appointmentObj[i], 'resource_ids', value.resource_uniqueids ); 
    _.set(appointmentObj[i], 'printable-string', `${value.title}, ${moment(value.created_at).format( 'Do MMMM YYYY')}` );
});    

linkedObjects.forEach((element, index) => {

    let isMatch = appointmentObj.find((obj) => {
        return _.includes(obj.resource_ids,element.id);
    });

    if(isMatch) {
        linkedObjects[index]['content']['printstring'] = `${currentMatch.printable-string}`;
    }
});
问题的出现是因为value.resource\u uniqueid可能是一个数组 包含许多ID,但我的测试只找到一个


Array.prototype.find从数组中返回单个匹配的元素。如果预期结果是迭代数组中的多个匹配结果,请使用Array.prototype.filter。

请注意,您可以编写linkedObjects[index]['content']['printstring']=。。。更清晰地显示为element.content.printstring=。。。。然后您可以从.forEach回调参数列表中删除索引,因为您不再使用索引。谢谢,我将尝试此操作。