简体   繁体   English

如何基于另一个数组中的键/值对为一个数组中的对象分配属性值?

[英]How do I assign property values for objects in one array based on key/value pairs in another?

I have an array that looks like this: 我有一个看起来像这样的数组:

var fields = [{Name: 'FirstName', Value: '', Display: 'First Name'}, {...}]

I have another array that looks like this: 我有另一个看起来像这样的数组:

var nameValue = [{'FirstName':'John'}, {'LastName':'Doe'}, ...]

How do I assign the Value property for each field in the first array using the name/value pairs in the second array? 如何使用第二个数组中的名称/值对为第一个数组中的每个字段分配Value属性?

Desired result: 所需结果:

fields = [{Name: 'FirstName', Value: 'John', Display: 'First Name'}, {...}]

You could create an object first with all needed values and iterate fields , make a check if a property with the name exists and then assign the value. 您可以先创建一个具有所有必需值和迭代fields的对象,检查是否存在具有名称的属性,然后分配该值。

 var fields = [{ Name: 'FirstName', Value: '', Display: 'First Name' }], nameValue = [{ FirstName: 'John' }, { LastName: 'Doe' }], values = Object.assign({}, ...nameValue); fields.forEach(o => (o.Name in values) && (o.Value = values[o.Name])); console.log(fields); 

Loop, check, assign: 循环,检查,分配:

fields.forEach(function(field) {
    let key = field.Name;
    for (let i = 0; i < nameValue.length; i++) {
        for (let nameKey in nameValue[i]) {
            if (nameKey == key) {
                field.Value = nameValue[i][key]
                break;
            }
        }
    }
});

First combine all the names and values into a single object: 首先将所有名称和值组合到一个对象中:

var nvObject = {};
nameValue.forEach(function (e) {
    for (key in e) {
        nvObject[key] = e[key];
    }
});

Now nvObject = { FirstName: 'John', LastName: 'Doe', ... } . 现在nvObject = { FirstName: 'John', LastName: 'Doe', ... } Perhaps you could construct nameValue like this in the first place, instead of making it an array of objects. 也许您可以首先构造像这样的nameValue ,而不是使其成为对象数组。

Then you can use this to update the other array. 然后,您可以使用它来更新另一个阵列。

$fields.forEach(function (e) {
    if (e.Name in nvObject) {
        e.Value = nvObject[e.Name];
    }
});

暂无
暂无

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

相关问题 如何分配字符串数组的键/值对 - How do i assign key / value pairs of an array of strings 如何将对象中的键值对转换为对象数组? - How do I turn key value pairs in object into array of objects? 如何基于在对象数组中共享另一个属性名称的值来对一个属性名称的值求和(在 Javascript 中) - How to sum the values of one property name based on sharing the value of another property name in an array of objects (in Javascript) 将对象数组转换为键值对,其中一个值作为键 - Transform an array of objects to key value pairs with one of the values as key 如何根据另一个键/值对数组过滤具有复杂嵌套对象的数组? - How to filter an array with complex nested objects based on another array of key/value pairs? 如何在数组中的键值对中声明和分配值 - How to declare and assign values to key value pairs in an array 有一个对象数组,需要添加新对象。 如何检查是否只有以下键之一:值对 - Have an array of objects, need to add new object. How do I check if only one of key: value pairs exist 如何根据另一个对象数组的值获取一个对象数组键的值? - how to get the values of one array of objects key based on values of another array of objects? 如何在javascript中将数组键和值对中的值分配给另一个数组? - how to assign values in array key and value pair one array to another array in javascript? 使用Lodash或Underscore JS,如何基于同一对象数组中的另一个键值获取特定的键值 - Using Lodash or Underscore JS , how do I obtain a specific key value based on another key value within the same objects array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM