简体   繁体   English

如何根据对象内的某些匹配项插入特定值

[英]How to insert particular value based on some matches inside an object

I have two objects with the following structure: 我有两个具有以下结构的对象:

var one = { 
    '26-34-1'  : 100,
    '26-34-84' : 101
};

var two = [ 
    { 
        from : { id: '34', sid: '26' },
        to   : { id: '1',  sid: '26' } 
    },
    { 
        from : { id: '34', sid: '26' },
        to   : { id: '84', sid: '26' } 
    }
];

The keys in one (eg. 26-34-1) identify an object in two in the format: one (例如26-34-1)中的键以two格式标识一个对象:
from.sid - from.id - to.id from.sid - from.id - to.id

What I need to do is insert the value from one into two if the key in one matches. 我需要做的是,如果one 匹配,则将值从one插入到two Resulting in: 导致:

var two = [ 
    { 
        from  : { id: '34', sid: '26' },
        to    : { id: '1',  sid: '26' },
        oneid : { id: 100 }
    },
    { 
        from  : { id: '34', sid: '26' },
        to    : { id: '84', sid: '26' },
        oneid : { id: 101 } 
    }
];

I can't seem to figure it out, I think it's done with "mapping" but wasn't able to map from.sid , from.id and to.id . 我似乎无法弄清楚,我认为它是通过“映射”完成的,但是无法映射from.sidfrom.idto.id Can someone please shed some light on this? 有人可以说明一下吗?

Edit : As I understand from another answer it's not what TS was asking about, my in case he doesn't have two yet next code will do what's needed. 编辑 :据我从另一个答案中了解到,这不是TS的要求,以防万一他没有two但下一个代码将满足所需。

for (var key in one) {
  var values = key.split('-');
  var object = {
    from: {
      id: values[1],
      sid: values[0]
    },
    to: {
      id: values[2],
      sid: values[0]
    },
    oneid: {
      id: one[key]
    }
  };

  two.push(object);
}
two.forEach(function(o){
    var key = [o.from.sid, o.from.id, o.to.id].join('-');
    if(key in one) o.oneid = { id: one[key] };
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 object 的特定数组中插入一个值 - How to insert a value inside a particular array of object 如何根据某些ID将值插入对象 - How to insert values into object based on some Id 如何根据某些特定条件动态调整div中元素的大小 - How to resize elements dynamically inside a div based on some particular conditions 如何根据某些条件更改对象值 - How to change an object value based on some conditions 如何在数组中插入 label 的 object 和值 - How to insert object of label and value inside array 如何基于其他数组遍历数组中的每个值并使用 javascript 检查它是否符合某些条件? - How to loop through each value in array based on other array and check if it matches some condition using javascript? 如何根据条件(例如 Node.js 中的值数组是否为空)有选择地向对象数组中的对象插入键 - How to selectively insert keys to an object inside an array of objects based on condition like whether the value array is empty or not in Node.js 如何根据 javascript 中的某个标志获取 object 属性的特定数组的总和? - How to get the sum of particular array of object property based on some flag in javascript? 如何使用基于字符串值的扩展运算符有条件地将 object 插入到数组中 - How to conditionally insert object to an array with the spread operator based on a string value 如何突出显示内部的特定行<pre>并根据特定单词进行迭代?</pre> - How to highlight particular line inside <pre> and iterate based on particular word?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM