1
var tempStoreFood = [];
var sameFoodID = [];
var differentFoodID = [];
var finalStoreFood = [];

console.log("Temporary stored food", tempStoreFood);
console.log("Same Food Group", sameFoodID);
console.log("Different Food Group", differentFoodID);
console.log("Concatenate");

for (i = 0; i < self.foods.length; i++) {
  var foodBrand = self.foods[i];
  var foodBrandID = self.foods[i].brand_id;
  tempStoreFood.push(foodBrand);
  for (j = 0; j < tempStoreFood.length; j++) {
    var foodID = tempStoreFood[j].brand_id;
    var singleFood = tempStoreFood[j];
    if (foodID == foodBrandID) {
      sameFoodID.push(singleFood);
      break;
    }
    if (foodID !== foodBrandID) {
      differentFoodID.push(singleFood);
      break;
    }
  }

out put of the code

from the output i have got the samefoodID array and different food id array. now i want to group it as this(final output). any idea how can i do this using javascript.

final output

Siva K V
  • 10,561
  • 2
  • 16
  • 29
RIYAL
  • 45
  • 1
  • 1
  • 9
  • Your screenshots don't show any details about what is actually in your arrays. Post your input and expected output in your question, not as images. – Cully Jun 13 '20 at 06:12
  • 1
    Does this answer your question? [How to merge two arrays in JavaScript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – Raul Sauco Jun 13 '20 at 06:23

5 Answers5

0

If you are expecting the final output as an array like,

arrayMergedFood = array(arraySameFood, arrayDifferentFood)

then create a new array arrayMergedFood, then push the array arraySameFood and arrayDifferentFood on to that array. Avoid mutation.

0

You can use spread operator to merge array.

var arr1 = [1,2,3,4]
var arr2 = [ ...arr1, 5,6,7,8]
console.log(arr2)
Santosh
  • 3,477
  • 5
  • 37
  • 75
0

You can use .concat() method

var array1 = [1, 2, 3];
var array2 = ['a', 'b', 'c'];
var array3 = array1.concat(array2);

console.log(array3);
Alyona Yavorska
  • 569
  • 2
  • 14
  • 20
0

var sameFoodID = [1,2,3];
var differentFoodID = [4,5];
var mergeResult = [...sameFoodID,...differentFoodID];

console.log(mergeResult);

The spread operator is a useful and quick syntax for adding items to arrays, combining arrays or objects.

karthicvel
  • 343
  • 2
  • 5
0

Although using the spread operator in combination with push or splice is terse and efficient, you will run into stack overflow errors for large arrays.

So if your data set is expected to be large, either:

  1. Always use concat.
  2. Check the size and then conditionally either use the spread operator with push or splice, or concat for larger arrays.

const data1 = Array(1_000).fill('X')
const data2 = Array(1_000_000).fill('X')

// Spread will work, but only up to a certain size
try {
  const target = []
  target.push(...data1)
  console.log(target.length)
} catch (e) {
  console.log(e.message)
}

// Spread will fail
try {
  const target = []
  target.push(...data2)
  console.log(target.length)
} catch (e) {
  console.log(e.message)
}

// Concat will work
try {
  const target = []
  const newTarget = target.concat(data2)
  console.log(newTarget.length)
} catch (e) {
  console.log(e.message)
}

// safeConcat will work
try {
  const target = []
  const maybeNewTarget = safeConcat(target, data2)
  console.log(maybeNewTarget.length)
} catch (e) {
  console.log(e.message)
}

// A safe concat that uses the faster spread operator.
function safeConcat(items, data) {
  // Max items will depend on your runtime
  if (data.length < 500_000) {
    items.push(...data)
    return items
  } else {
    return items.concat(data)
  }
}
Steven Spungin
  • 27,002
  • 5
  • 88
  • 78