How to merge new data to old array?

merge arrays javascript
javascript merge array of objects
javascript merge array of objects by key
javascript merge two arrays without duplicates
php merge array values with same keys
array merge
php merge multidimensional array
merge two arrays with same keys javascript

I have a array like this. Here I have included only 5 fields but in real I have around 45 fieldname.

0: {id:1, product_name: 'aaa', val1:1, val2:2, val3:4}
1: {id:2, product_name: 'bbb', val1:1, val2:2, val3:4}
2: {id:3, product_name: 'ccc', val1:1, val2:2, val3:4}
3: {id:4, product_name: 'ddd', val1:1, val2:2, val3:4}

I want to get the sum of val1, val2 and val3 and display in total column and append '%' sign to this 3 value too.

I did following to get the sum and append %

temp = temp.map((row) => {
    const newRow= {};
    let total = 0;

    total = row['val1'] +
            row['val2'] +
            row['val3'] +

    newRow['total'] = total;

    newRow['val1'] = row['val1'] + '%';
    newRow['val2'] = row['val2'] + '%';
    newRow['val3']   = row['val3'] + '%';

    return newRow;
});

With this code I return only val1, val2 and val3 in my array. How can I append other 42 columns and get the final result.

You can do like that

var temp = [{id:1, product_name: 'aaa', val1:1, val2:2, val3:4},
{id:2, product_name: 'bbb', val1:1, val2:2, val3:4},
{id:3, product_name: 'ccc', val1:1, val2:2, val3:4},
{id:4, product_name: 'ddd', val1:1, val2:2, val3:4}];

temp = temp.map(row => {
  const newRow = row;
  let total = 0;

  total = parseInt(row['val1']) +
    parseInt(row['val2']) +
    parseInt(row['val3']);

  newRow['total'] = total;

  newRow['val1'] = row['val1'] + '%';
  newRow['val2'] = row['val2'] + '%';
  newRow['val3'] = row['val3'] + '%';

  return newRow;
});
$("#result").append(JSON.stringify(temp) + "<br />");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='result'>

</div>

Merge 2 arrays of objects, How do I merge two arrays of objects into a new one? So here's the quick rule. If you know you're dealing with arrays, use spread. But if you might be dealing with the possibility with a non-array, then use concat to merge an array 👍 Anyways I just want to point that out, so you can use the most appropriate method depending on the problem you're trying to solve 👍 # Merge Array with Push 🤔

You could take an array of the wanted keys and map new properties for a single array.

var data = [{ id: 1, product_name: 'aaa', val1: 1, val2: 2, val3: 4 }, { id: 2, product_name: 'bbb', val1: 1, val2: 2, val3: 4 }, { id: 3, product_name: 'ccc', val1: 1, val2: 2, val3: 4 }, { id: 4, product_name: 'ddd', val1: 1, val2: 2, val3: 4 }],
    keys = ['val1', 'val2', 'val3'],
    result = data.map(o => Object.assign(...keys.map(
        (t => k => ({ total: t += o[k], [k]: o[k] + '%' }))
        (0)
    )));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How To Concatenate Arrays in NumPy?, How do you merge two arrays into one while also remove duplicates? Given two arrays of the same type, they need to be merged into a new object array. The task is to merge the two arrays of the same type into an object array such that the array elements maintain their original order in the newly merged array and the elements of the first array precedes the elements of the second array in the merged object array.

You need to change your definition of const newRow to such:

let temp = this.some.map((row) => {
  const newRow = {...row};
  let total = 0;
  total = row['val1'] +
          row['val2'] +
          row['val3'];
  newRow['total'] = total;

  newRow['val1'] = row['val1'] + '%';
  newRow['val2'] = row['val2'] + '%';
  newRow['val3'] = row['val3'] + '%';

  return newRow;
});

Merge Two Arrays without Duplicate Values in PHP?, of the same shape and by default it concatenates row-wise i.e. axis=0. C Program to Merge Two Arrays Example 1. This program to merge two arrays in c allows the user to enter the Array size, Array elements of two different arrays. Next, it will merge two arrays one after the other using For Loop.

update/merge array values in React Redux store correctly without , How can I merge two arrays in PHP without duplicates? There are following ways to merge two arrays: Java arraycopy() method; Without using arraycopy() method; Java Collections; Java Stream API; Java arraycopy() method. Java arraycopy() is the method of System class which belongs to java.lang package. It copies an array from the specified source array to the specified position of the destination array.

Merging and Deduplicating Data Arrays with Array.reduce, I would filter out elements of the old data that are in the new data, and concat. const oldBooks = booksData.filter(book => ! The middle tier can then send back either a new DataSet that includes original data and the latest data from the data source (by running the original query again), or it can send back the subset with any changes that have been made to it from the data source. (For example, if the data source automatically creates unique primary key values

array_merge - Manual, Merging Data with Array. reduce. Timestamp each project when passing it into the reducer. Concat the old state array with the new, timeStamped projects in action. projects. Sort the data by IDs. 'Walk' the data using array. Reduce(), starting by passing in an array containing the first project in the concatenated array. The idea is to use Merge function of Merge sort. Create an array arr3 [] of size n1 + n2. Simultaneously traverse arr1 [] and arr2 []. Pick smaller of current elements in arr1 [] and arr2 [], copy this smaller element to next position in arr3 [] and move ahead in arr3 [] and the array whose element is picked.

Comments
  • You can use reduce().
  • @CodeManiac can you show me?
  • @how the product name output looks line ? is it in array or string concatenated
  • @codemaniac its an array
  • later I need to show id and product_name too in above case.
  • Object.assign({},o,...keys.map would have the original props included without mutating the original.