简体   繁体   English

使用JS / TS将数组中对象的所有属性插入数组中的另一个对象

[英]Insert all properties from an object within an array to another object in array using JS/TS

I have been looking a simple way to copy/insert/move properties in an object within an array to another object.我一直在寻找一种简单的方法来将数组中的对象中的属性复制/插入/移动到另一个对象。 I came up with a basic logic which does the job perfectly but am not satisfied with this.我想出了一个基本逻辑,它完美地完成了这项工作,但对此并不满意。 There has to be a better way, any help here?必须有更好的方法,这里有什么帮助吗?

var first =  [
    {
        "AGREE_EFF_DATE__0": "02-Aug-2018",
        "AGREE_TERM_DATE__0": "30-Apr-2021",
        "AGREE_IND__0": "P1",
        "P_DBAR_IND__0": "N",
        "AGREE_EFF_DATE__1": "01-May-2021",
        "AGREE_TERM_DATE__1": null,
        "AGREE_IND__1": "NP",
        "P_DBAR_IND__1": "N",
        "PROVIDER_SPECIALITY__0": "PSYCHOLOGY, CLINICAL",
        "PROVIDER_SPECIALITY_CODE__0": "CK"
    }
];
var second = [
    {
        "STATUS": "ACTIVE",
        "MEDICARE_NUMBER" : 12345
    }
];

for(let i = 0; i < second.length; i++) {
    
    var first_keys = Object.keys(first[i]);
    var first_values = Object.values(first[i]);
    
    for(let j = 0; j < first_keys.length; j++) {
        second[i][first_keys[j]] = first_values[j];
    }
}


console.log(second);

//Output-
[
  {
    STATUS: 'ACTIVE',
    MEDICARE_NUMBER: 12345,
    AGREE_EFF_DATE__0: '02-Aug-2018',
    AGREE_TERM_DATE__0: '30-Apr-2021',
    AGREE_IND__0: 'P1',
    P_DBAR_IND__0: 'N',
    AGREE_EFF_DATE__1: '01-May-2021',
    AGREE_TERM_DATE__1: null,
    AGREE_IND__1: 'NP',
    P_DBAR_IND__1: 'N',
    PROVIDER_SPECIALITY__0: 'PSYCHOLOGY, CLINICAL',
    PROVIDER_SPECIALITY_CODE__0: 'CK'
  }
]

When possible, you should prefer iteration to manually indexed loops.如果可能,您应该更喜欢迭代而不是手动索引循环。 This means arr.map() or arr.forEach() or arr.reduce() , to name a few.这意味着arr.map()arr.forEach()arr.reduce() ,仅举几例。

Also, You can use an object spread to easily merge objects together.此外,您可以使用对象展开轻松地将对象合并在一起。

Putting those together, you can reduce this logic to:将它们放在一起,您可以将此逻辑简化为:

const result = first.map((firstObj, i) => ({ ...firstObj, ...second[i] }))

Here we map() over all members of first , which returns a new array where each member is the result of the function.这里我们map()覆盖first的所有成员,它返回一个新数组,其中每个成员都是函数的结果。 This function takes the array member as the first argument, and the index of that member as the second argument.该函数将数组成员作为第一个参数,将该成员的索引作为第二个参数。 Then we can use that index to find the corresponding item in the second array.然后我们可以使用该索引在第二个数组中找到相应的项目。

Then you just spread both objects into a new object to assemble the final result.然后,您只需将两个对象分散到一个新对象中即可组合最终结果。

 var first = [ { a: 1, b: 2 }, { a: 4, b: 5 }, ]; var second = [ { c: 3 }, { c: 6 }, ]; const result = first.map((firstObj, i) => ({ ...firstObj, ...second[i] })) console.log(result)

Which is all perfectly valid typescript as well.这也是完全有效的打字稿


NOTE: there is one difference between my code any yours.注意:我的代码与您的代码有一个区别。 Your code modifies the objects in second .您的代码在second中修改了对象。 My code returns new objects and does not change the contents of second at all.我的代码返回新对象,根本不改变second的内容。

This is usually the better choice, but it depends on how you use this value and how data is expected to flow around your program.这通常是更好的选择,但这取决于您如何使用此值以及数据如何在您的程序中流动。

You need to be careful with iterating, because you can have different count of elements in first and second arrays.您需要小心迭代,因为您可以在第一个和第二个数组中拥有不同数量的元素。 So the possible solution will be like this:所以可能的解决方案是这样的:

 const first = [ { "AGREE_EFF_DATE__0": "02-Aug-2018", "AGREE_TERM_DATE__0": "30-Apr-2021", "AGREE_IND__0": "P1", "P_DBAR_IND__0": "N", "AGREE_EFF_DATE__1": "01-May-2021", "AGREE_TERM_DATE__1": null, "AGREE_IND__1": "NP", "P_DBAR_IND__1": "N", "PROVIDER_SPECIALITY__0": "PSYCHOLOGY, CLINICAL", "PROVIDER_SPECIALITY_CODE__0": "CK" } ]; const second = [ { "STATUS": "ACTIVE", "MEDICARE_NUMBER": 12345 } ]; console.log(mergeAll(first, second)); function mergeAll(firstArray, secondArray) { const result = []; const minLength = firstArray.length < secondArray.length ? firstArray.length : secondArray.length; for (let i = 0; i < minLength; i++) { result.push({...firstArray[i], ...secondArray[i]}); } return result; }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM