在具有复杂条件的两个对象数组之间检索项目

我有两个对象列表:


let list1 = [{id: '1', status: 'use', comment: 'xxxx'}, {id: '2', status: 'ready', comment: 'yyyy'}, {id: '3', status: 'ready', comment: 'zzzz'}];

let list2 = [{uid: '1', elec: 60}, {uid: '2', elec: 60}, {uid: '10', elec: 60}, {uid: '3', elec: 40}];


我想要的是检索 list2 的一个对象,该对象具有 elec > 50 且 uid 与 list1 的一项 id 相同,前提是 list1 的该项具有状态==“就绪”。另外,我想向此项目添加来自 list1 对象的参数“comment”。


在此示例中,我的结果值为:{uid: '2', elect: 60, comment: 'yyyy'}。


我这样做了:


  let list1Filtered = list1.filter(itemList1 => itemList1.status == 'ready');

  let list2Filtered = list2.filter(itemList2 => itemList2.elec > 50);

  var result;


  for ( let  itemList1Filtered of list1Filtered ) {

    for ( let  itemList2Filtered of list2Filtered ) {

      if (!result && itemList1Filtered.id == itemList2Filtered.uid) {

        result = itemList2Filtered;

        result.comment = itemList1Filtered.comment;

      }

    }

  }

  

  return result;

我想知道是否有更优雅和/或更复杂的方法可以在 Javascript 中执行此操作。


ibeautiful
浏览 119回答 4
4回答

莫回无

您可以从 list1 中收集想要的评论,并通过检查其他列表中是否存在某个项目list2的值来减少。elec然后返回一个新对象。这种方法只需要两个循环。const    list1 = [{ id: '1', status: 'use', comment: 'xxxx' }, { id: '2', status: 'ready', comment: 'yyyy' }, { id: '3', status: 'ready', comment: 'zzzz' }],    list2 = [{ uid: '1', elec: 60 }, { uid: '2', elec: 60 }, { uid: '10', elec: 60 }, { uid: '3', elec: 40 }],    l1 = list1.reduce((r, { id, status, comment }) => {        if (status === 'ready') r[id] = { comment };        return r;    }, {}),    result = list2.reduce((r, o) => {        if (o.elec > 50 && o.uid in l1) r.push({ ...o, ...l1[o.uid]})        return r;    }, []);console.log(result);

慕丝7291255

let result = {    ...list2.filter(    a => a.elec > 50 && a.uid === list1.filter(b => b.status === "ready")[0].id)[0],    comments: list1.filter(b => b.status === "ready")[0].comment}

紫衣仙女

you can restructure your data. List1 convert it in object. And now we can find solution in O(n).let list1 = [{  id: '1',  status: 'use',  comment: 'xxxx'}, {  id: '2',  status: 'ready',  comment: 'yyyy'}, {  id: '3',  status: 'ready',  comment: 'zzzz'}];let list2 = [{  uid: '1',  elec: 60}, {  uid: '2',  elec: 60}, {  uid: '10',  elec: 60}, {  uid: '3',  elec: 40}];const itemList = {}list1.forEach(item => {  if (item.status === 'ready') {    itemList[item.id] = item.comment  }});const result = list2.filter(item => itemList[item.uid] && item.elec > 50).map(item => {  item['comment'] = itemList[item.uid]  return item})console.log(result)

慕妹3242003

尝试这个:let list1 = [  { id: '1', status: 'use', comment: 'xxxx' },  { id: '2', status: 'ready', comment: 'yyyy' },  { id: '3', status: 'ready', comment: 'zzzz' },];let list2 = [  { uid: '1', elec: 60 },  { uid: '2', elec: 60 },  { uid: '10', elec: 60 },  { uid: '3', elec: 40 },];let result = null;let itemFound;const filteredList = list2.filter((list2Item) => {  if (!result) {    itemFound =      list2Item.elec > 50 &&      list1.find(        (list1Item) =>          list2Item.uid === list1Item.id && list1Item.status === 'ready'      );    if (itemFound) {      result = {        uid: list2Item.uid,        elect: list2Item.elec,        comment: itemFound.comment,      };    }  }});console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript