2017-10-18 3 views
-4

Je voudrais concatuer 2 tableaux dans JSON avec clé et valeur.2 Tableaux dans JSON

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}, 
    ] 
    ] 

J'ai essayé, mais je besoin d'une clé et de la valeur et concat mes 2 tableaux:

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; 
        } 
       }); 
+5

Avez-vous essayé quoi que ce soit? –

+2

JSON est une * notation textuelle * pour l'échange de données. [(Plus ici.)] (Http://stackoverflow.com/a/2904181/157247) Si vous traitez avec du code source JavaScript, et non avec une * chaîne *, vous n'avez pas affaire à JSON. –

+0

Ce que vous voulez faire est de diviser les chaînes "Orange: 10" etc. en différentes valeurs, ce n'est pas juste de concaténer des tableaux. – Danmoreng

Répondre

0

Comme promis, voici une nouvelle version qui résume les valeurs du même fruit. Notez que les valeurs sont des nombres entiers plus pratiques à ajouter. Quoi qu'il en soit, si vous voulez absolument des chaînes utilisez arr [i] .value = arr [i] .value.toString(); S'il vous plaît me donner un avis.

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) { 
 
\t var items = item.split(":"); 
 
\t item = {}; 
 
\t item.fruit = items[0]; 
 
\t item.value = parseInt(items[1]); 
 
\t return item; 
 
}); 
 
return arr2; 
 
} 
 
    
 
    
 
function mergeObjectArray(arr,compareKey,addKey){ 
 
\t // Pay attention : we loop thru the same arr searching from end to start (i) 
 
\t // and check if the same fruit already exist (second loop j from start to one row before i) 
 
\t // that way we can sum any number of concated arrays and even dupes in the same original array 
 
    \t for(var i = arr.length-1; i >=0;i--){ 
 
\t \t 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] 
 
\t \t \t if((arr[i][compareKey]==arr[j][compareKey]) && (i!=j)){ // if the fruit (compare key) is the same (has been found) 
 
\t \t \t \t arr[j][addKey]+=arr[i][addKey]; // we sum 
 
\t \t \t \t arr.splice(i, 1); // we get rid of row (from end to start i, not to mess with the offset of the loop) 
 
\t \t \t \t break; 
 
\t \t \t } 
 
\t \t } \t 
 
\t } 
 
return arr; 
 
}

+0

Merci pour votre aide! – pifou

-1

Voici un exemple rapide: je fais une fonction simple juste pour transformer le tableau plat dans un tableau d'objets, puis je mets les deux tableaux dans un autre tableau de repos

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; 
 
}

+0

Merci, mais je voudrais ajouter le même élément pour chaque tableau – pifou

+0

Je ne comprends pas: n'avez-vous pas montré ce que vous attendiez dans MyJSON? Vous voulez seulement un tableau d'objets sur les 2 tableaux plats? – PhilMaGeo

+0

si c'est ce que vous voulez faire: var arr3 = myArray1.concat (myArray2); var myResult = arrayToObjectArray (arr3); – PhilMaGeo