-4

I'd like to concat 2 arrays in JSON with key and value.

MyArray1 [ "Orange:10", "Orange:5", "Banana:20", "Apple:5" ]
MyArray2 [ "Orange:5", "Banana:10", "Apple:15" ]

MyJSON   [
      {"fruit": "Orange", "value": 15},
      {"fruit": "Banana", "value": 20},
      {"fruit": "Apple ", "value": 5},
    ],[
      {"fruit": "Orange", "value": 5},
      {"fruit": "Banana", "value": 10},
      {"fruit": "Apple ", "value": 15},
    ]
  ]

I've tried this but I need a key and value and concat my 2 arrays :

MyArray1.forEach(function(val) {
                    var item = val.split(":");
                    var key = item[0];
                    var num = parseInt(item[1], 10);

                    if (MyArray1.hasOwnProperty(key)) {
                    MyArray1[key] += num;
                    } else {
                        MyArray1[key] = num;
                    }
                }); 
pifou
  • 47
  • 6
  • 5
    Have you tried anything at all? – Felix Kling Oct 18 '17 at 15:34
  • 2
    JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder Oct 18 '17 at 15:34
  • What you want to do includes splitting the strings "Orange:10" etc. in different values, it's not just concatting arrays. – Danmoreng Oct 18 '17 at 15:38
  • I've tried this `MyArray1.forEach(function(val) { var item = val.split(":"); var key = item[0]; var num = parseInt(item[1], 10); if (MyArray1.hasOwnProperty(key)) { MyArray1s[key] += num; } else { MyArray1[key] = num; } });` – pifou Oct 18 '17 at 15:41

2 Answers2

0

As promised, here is a new version that sums up the values of the the same fruit Note that the values are integer which is more convenient for adding. Anyway if you absolutely want strings use arr[i].value=arr[i].value.toString() ; Please give me a feedback.

var myArray1 =  [ "Orange:10", "Orange:5", "Banana:20", "Apple:5" ];
var myArray2 = [ "Orange:5", "Banana:10", "Apple:15" ];

var myObjectArray1 = arrayToObjectArray(myArray1);
var myObjectArray2 = arrayToObjectArray(myArray2);

var myOnlyOneObjectArray= myObjectArray1.concat(myObjectArray2);

var myResult = mergeObjectArray(myOnlyOneObjectArray,"fruit","value")

console.log(myResult);

function arrayToObjectArray(arr){
// Map allows us to return a transformed row for each array row
var arr2 = arr.map(function(item) {
 var items = item.split(":");
 item = {};
 item.fruit = items[0];
 item.value = parseInt(items[1]);
 return item;
});
return arr2;
}
 
 
 function mergeObjectArray(arr,compareKey,addKey){
 // Pay attention : we loop thru the same arr searching from end to start (i)
 // and check if the same fruit already exist (second loop j from start to one row before i)
 // that way we can sum any number of concated arrays and even dupes in the same original array
  for(var i = arr.length-1; i >=0;i--){
  for(var j = 0; j < arr.length -1;j++){ // Note that an objet property can be accessed also this way arr[i]["fruit"] == arr[i].fruit == arr[i][compareKey]
   if((arr[i][compareKey]==arr[j][compareKey]) && (i!=j)){ // if the fruit (compare key) is the same (has been found)
    arr[j][addKey]+=arr[i][addKey];  // we sum 
    arr.splice(i, 1); // we get rid of row (from end to start i, not to mess with the offset of the loop)
    break;
   }
  } 
 }
return arr;
}
PhilMaGeo
  • 551
  • 6
  • 8
-1

Here is a quick example : I make a simple function just to transform the flat array into an array of objects and then I put the two resting arrays into another array

var myArray1 =  [ "Orange:10", "Orange:5", "Banana:20", "Apple:5" ];
var myArray2 = [ "Orange:5", "Banana:10", "Apple:15" ];

var myArray1b = arrayToObjectArray(myArray1);
var myArray2b = arrayToObjectArray(myArray2);

var myResult = [myArray1b,myArray2b];

console.log(myResult);

function arrayToObjectArray(arr){
var arr2 = arr.map(function(item) {
var items = item.split(":");
item = {};
item.fruit = items[0];
item.value = items[1];

return item;
});
return arr2;
}
PhilMaGeo
  • 551
  • 6
  • 8
  • Thanks, but I would like to add the same item for each array – pifou Oct 18 '17 at 16:05
  • I don't get : didn't you show what you expected in MyJSON ? You want only one array of objects out of the 2 flat arrays ? – PhilMaGeo Oct 18 '17 at 16:30
  • if that's what you want just do : var arr3 = myArray1.concat(myArray2); var myResult = arrayToObjectArray(arr3); – PhilMaGeo Oct 18 '17 at 16:33
  • Your code works but in the JSON, the first object must be `{ "fruit": "Orange", "value": "15" },` and not `{ "fruit": "Orange", "value": "10" }, { "fruit": "Orange", "value": "5" },` I'd like to sum the 2 items "orange" – pifou Oct 18 '17 at 16:46
  • i understand you want to merge the 2 arrays if the fruit is the same. No problem : i'll will show – PhilMaGeo Oct 18 '17 at 18:21