Object reorganization of js array

arr = [

]
{"param":"CASH_LIMIT"},
{"param":"green"},
{"regular":"="},
{"regular":"!="},
{"value":"15000"},
{"value":"2323"}

]

changeArr = [

]
{"param":"CASH_LIMIT","regular":"=","value":"15000"},
{"param":"green","regular":"!=","value":"2323"},  

]

how to traverse to turn arr into changeArr

Jun.23,2021

function trans (arr) {
  return arr.reduce((sum, cur, index) => {
    let keys = Object.keys(cur)
    let tmp = sum[index % 2]
    keys.forEach(key => {
      tmp[key] = cur[key]
    })
    return sum
  }, [{}, {}])
}
trans(arr)

I don't know what function you want to achieve? If you simply want to integrate objects, and if you do as shown above in the arr array, then you can just copy the number of objects according to the number of objects. As shown in the following code:

function funchan(arr,count)
{ 
    if (!Array.isArray(arr)) return null; 
    let len = arr.length; 
    let newLen = len / count; 
    let result = new Array();  
    for (let i =0; i < newLen; iPP) 
    { 
        let temp = {}; 
        for (let j = 0; j < count; jPP)
        { 
            temp = Object.assign(temp,arr[j*newLen + i]);
        } 
        result.push(temp);   
    }
    return result;
}

clipboard.png


the rule seems to be odd in the first object and even in the second object

Menu