简体   繁体   English

如何根据另一个对象数组的值获取一个对象数组键的值?

[英]how to get the values of one array of objects key based on values of another array of objects?

   data = {
{'uid': 12, 'amount': 100},
{'uid': 23, 'amount': 250}
}
object = {12:{'name':'Paul', 'id':12}, 20:{'name':'Mike', 'id':20}, 41:{'name':'Jack', 'id':41}, 23:{'name':'Luke', 'id':23}}

the output expected is预期的 output 是

const result = {{'name':'Paul', 'id': 12, 'amount':100}, {'name':'Luke', 'id': 12, 'amount': 250}}

I tried mapping the uids to get and array of values like我尝试映射 uid 以获取和数组值,例如

uids = [12, 23]

then然后

        names = uids.map(key => object[key].name);

so that I could use them to map the keys and the names in the other object but couldn't figure it out I know it is basic javascript but I am new to this这样我就可以使用它们来 map 其他 object 中的键和名称,但无法弄清楚我知道这是基本的 javascript 但我是新手

You could simply map the object with the given keys.您可以使用给定的键简单地 map object。

 var array = [12, 23], object = { 12: { name: 'Paul', id: 12 }, 20: { name: 'Mike', id: 20 }, 41: { name: 'Jack', id: 41 }, 23: { name: 'Luke', id: 23 } }, result = array.map(key => object[key]); console.log(result);

var data = {
{'uid': 12, 'amount': 100},
{'uid': 23, 'amount': 250}
};

var object = {12:{'name':'Paul', 'id':12}, 20:{'name':'Mike', 'id':20}, 41:{'name':'Jack', 'id':41}, 23:{'name':'Luke', 'id':23}};

var uids = [12, 23];

    mappedValues = uids.map(key => object[key]);

 var result = mappedValues.map((item, i) => Object.assign({}, item, data[i]));
console.log(result);

First of all the structure of data is wrong.首先data的结构是错误的。 It should be an array.它应该是一个数组。 Then you could do this:然后你可以这样做:

 data = [ {'uid': 12, 'amount': 100}, {'uid': 23, 'amount': 250} ]; object = {12:{'name':'Paul', 'id':12}, 20:{'name':'Mike', 'id':20}, 41:{'name':'Jack', 'id':41}, 23:{'name':'Luke', 'id':23}}; for(let key in object){ var match = data.filter(i=>i.uid.toString() === key); if(match && match.length){ object[key].amount = match[0].amount; } } console.log(object)

暂无
暂无

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

相关问题 如何从一个对象数组中获取值到另一个对象数组中 - How to get values from one array of objects into another array of objects 如何基于另一个数组中的键/值对为一个数组中的对象分配属性值? - How do I assign property values for objects in one array based on key/value pairs in another? 如何根据 ReactJs 中的另一个数组将对象数组修改为新值? - How to modify an array of objects to new values based on another array in ReactJs? 如何根据键匹配值将两个对象数组组合为一个数组 - How to combine two array of objects into one array based on key match values VueJS根据监视键从对象数组中获取唯一值 - VueJS get unique values from array of objects based on key in watch 根据来自另一个对象数组的多个值过滤对象数组 - Filter array of objects based on multiple values from another array of objects 在另一个对象数组中搜索值列表,并使用single for循环更改对象数组中的一个键 - Search the list of values in another array of objects and change the one key in array of objects using single for loop 如何获取对象数组中的值? - How to get values in an array of objects? 如何从一个键上包含一个数组的对象数组中获取不同的值并计算 JavaScript? - How to get distinct values from an array of objects which has an Array inside in on one key and count JavaScript? 如何使用另一个数组获取对象数组中的值 - Javascript - How to get values in an array of objects using another array - Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM