简体   繁体   English

根据值将一个带有对象的数组中的属性添加到另一个数组

[英]Add property from one array with objects to another based on value

I have two arrays of objects like this:我有两个 arrays 对象,如下所示:

let test = [
{Course: "", Week: "1", Hours: ""}, 
{Course: "", Week: "2", Hours: ""}, 
{Course: "", Week: "3", Hours: ""}
]

let values = [{Course: "A", Week: "3", Hours: "1"}]

I want to add the property from array "values" to array "test so the result will be:我想将数组“values”中的属性添加到数组“test”中,结果将是:

let test = [
{Course: "", Week: "1", Hours: ""}, 
{Course: "", Week: "2", Hours: ""}, 
{Course: "", Week: "3", Hours: "1"}
]

Is this possible?这可能吗? The reason why I have it like this, is I need to specify all weeks at the start, and the values will be added from the user.我之所以这样,是因为我需要在开始时指定所有周,并且这些值将从用户那里添加。

First make an object mapping weeks to objects for fast lookup then just loop through the values array:首先制作一个 object 映射周到对象以便快速查找,然后循环遍历values数组:

const lookup = {};
for (const val of test) {
    lookup[val.week] = val;
}
for (const val of values) {
    const cur = lookup[val.week];
    for (const key in val) {
        if (key === "Hours") {
            cur[key] = (Number(cur[key]) + Number(val[key])).toString();
        } else {
            cur[key] = val[key];
        }
    }
}

Loop through Values - Find the test that matches current value's week and set hours to value's hours循环值 - 查找与当前值的周匹配的测试并将小时数设置为值的小时数

values.forEach((value) => {
    //Find the test that matches current value's week and set hours to value's hours
    test.find(test => test.Week === val.Week).Hours = value.Hours);
});

You can just use array.find function.您可以使用 array.find function。

 let test = [ {Course: "", Week: "1", Hours: ""}, {Course: "", Week: "2", Hours: ""}, {Course: "", Week: "3", Hours: ""} ] let values = [{Course: "A", Week: "3", Hours: "1"}] var current = test.find((item) => { return item.Week === values[0].Week }) current.Hours += values[0].Hours; console.log(test)

暂无
暂无

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

相关问题 如何在 JS 中将一个对象数组的属性值添加到另一个对象数组(用于匹配对象) - How to add property value from one array of objects into another (for the matching objects) in JS "根据属性值 lodash 将对象数组中的属性合并到另一个对象中" - Merge property from an array of objects into another based on property value lodash 如何基于在对象数组中共享另一个属性名称的值来对一个属性名称的值求和(在 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) 如何基于另一个数组中的键/值对为一个数组中的对象分配属性值? - How do I assign property values for objects in one array based on key/value pairs in another? 根据另一个对象数组的属性值对数组进行排序 - Sorting an array based on property value of another array of objects 将对象的属性值添加到对象数组 - Add property value from object to array of objects 根据来自不同对象数组的属性和值过滤对象数组 - Filter an array of objects based on a property and value from a different array of objects 将属性从一个对象数组添加到另一个对象 - Add properties from one array of objects to another 根据JavaScript中另一个“属性值”从数组中选择“属性值” - Selecting a 'property value' from an array based on another 'property value' in javascript 根据另一个数组中值的存在向对象数组添加一个属性 - Add a property to array of object based on the existence of value in another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM