简体   繁体   中英

How to add a field to each object in array and push that result array to another array using react and javascript?

i want to add a field type to each object in an array and push the result to another array and push that to another array.

below is how the array of object looks,

const arrObj1 = [
    {
        id: '1',
        name: 'n1',
    },
    {
        id: '2',
        name: 'n2',
    }
]

const arrObj2 = [
    {
        id: '3',
        name: 'n3',
    },
    {
        id: '4',
        name: 'n4',
    }
]

what i am trying to do?

i want to add a type field with value 'type1' to arrObj1 and type field with value 'type2' to arrObj2

so the output should be like below,

const arrObj1 = [
    {
        id: '1',
        name: 'n1',
        type: 'type1',
    },
    {
        id: '2',
        name: 'n2',
        type: 'type1',
    }
]

const arrObj2 = [
    {
        id: '3',
        name: 'n3',
        type: 'type2',
    },
    {
        id: '4',
        name: 'n4',
        type: 'type2',
    }
]

then i want to combine these two arrays arrObj1 and arrObj2 into one array like below

const combinedArrObj = [
    {
        id: '1',
        name: 'n1',
        type: 'type1',
    },
    {
        id: '2',
        name: 'n2',
        type: 'type1',
    },
    {
        id: '3',
        name: 'n3',
        type: 'type2',
    },
    {
        id: '4',
        name: 'n4',
        type: 'type2',
    }
]

what i have tried?

const arrObj1Res = arrObj1.map((obj: any) => {
    return {
        ...arrObj1,
        type: 'type1',
    }
});
const arrObj2Res = arrObj2.map((obj: any) => {
    return {
        ...arrObj2,
        type: 'type2',
    }
});
const combinedArrObj = [
    ...arrObj1Res,
    ...arrObj2Res,
];

this works. but how can i refactor to something simple rather than adding field type for each obj in array and then combine

could someone help me with this. thanks.

you can use this

let result=arrObj1.concat(arrObj2)
function contains(a, obj) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] === obj) {
            return true;
        }
    }
    return false;
}
result.filter(element => {
      if (contains(arrObj1,element) ){
        element.type='type1'
        return element
      }else {
        element.type='type2'
        return element
      }
});

In pure JS, just a simple for loop can do the part where you add a field named type.

for(var i = 0; i < arrObj1.length; i++){
 arrObj1[i].type = "type1";
}
for(var i = 0; i < arrObj2.length; i++){
 arrObj2[i].type = "type2";
}

and to combine the arrays together just use concat.

const combinedArrObj = arrObj1.concat(arrObj2);
const allArrayObject = arrObj1.concat(arrObj2);


const finalResultArray = allArrayObject.reduce((finalResultArray, eachArrayObj, index) =>{
     const isTypeOne =  arrObj1.length - 1 >= index
     const relevantTypeString = isTypeOne ? "type1" : "type2"

     eachArrayObj.type = relevantTypeString
     finalResultArray.push(eachArrayObj)  
    
     return finalResultArray
}, [])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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