sonja
sonja

Reputation: 934

Merge object to a multidimensional array based on a common value

I'm looking for a way to merge an object into an array based on a common value. My solution should be dynmic, which causes me a lot of troubles. Here is an example:

I have a multidimensional array (see codebelow). It contains a column called "Währung", which has numeric values.

0: {AGIMENDO Info Objekt 1: "00000000", Beschreibung Kurz: "Test0", Währung: "200.00", __rowNum__: 1}
1: {AGIMENDO Info Objekt 1: "00000001", Beschreibung Kurz: "Update1", Währung: "456.00", __rowNum__: 2}
2: {AGIMENDO Info Objekt 1: "00000002", Beschreibung Kurz: "Test2", Währung: "12.00", __rowNum__: 3}
3: {AGIMENDO Info Objekt 1: "00000003", Beschreibung Kurz: "Test3", Währung: "549153.00", __rowNum__: 4}
4: {AGIMENDO Info Objekt 1: "00000004", Beschreibung Kurz: "Text", Währung: "1.05", __rowNum__: 5}
5: {AGIMENDO Info Objekt 1: "00000005", Beschreibung Kurz: "13.08.11", Währung: "465.00", __rowNum__: 6}
6: {AGIMENDO Info Objekt 1: "00000006", Beschreibung Kurz: "Test21", Währung: "4594.00", __rowNum__: 7}

For each row, I iterated through each cell and assigned a "type" to each cell that contains a numeric/date value. The codebelow shows that array. Now I want to merge these two arrays based on the common value (for the first array it's "währung" and the second array it's "value). I tried using lodash _.map, _.assign etc. but I don't get the output I want.

0: {type: "number", value: "200.00"}
1: {type: "number", value: "456.00"}
2: {type: "number", value: "12.00"}
3: {type: "number", value: "549153.00"}
4: {type: "number", value: "1.05"}
5: {type: "date", value: "13.08.11"}
6: {type: "number", value: "465.00"}

My target output would look like this (for the first row):

> 0:
>   AGIMENDO Info Objekt1: "00000000"
>   Beschreibung Kurz: "Test0" 
>   Währung:
>     Value : "200.00" 
>     Type: "number"

How do I achieve this?

Upvotes: 0

Views: 80

Answers (3)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10960

This should work as you could update the json array directly

json_arr = [{"AGIMENDO Info Objekt 1": "00000000", "Beschreibung Kurz": "Test0", "Währung": "200.00", "__rowNum__": 1},
 {"AGIMENDO Info Objekt 1": "00000001", "Beschreibung Kurz": "Update1", "Währung": "456.00", "__rowNum__": 2}]

function update_type(json_arr, column_name) {    
    for(var i=0; i<json_arr.length; i++){
        element = json_arr[i][column_name];
        json_arr[i][column_name] = {};
        json_arr[i][column_name]['value'] = element;
        json_arr[i][column_name]['type'] = get_type(json_arr[i][column_name]); /* Here comes your type checking function */
   }
   return json_arr
}
console.log(update_type(json_arr, "Währung"))

Upvotes: 0

barbsan
barbsan

Reputation: 3458

You have to loop through all properties and replace value if second array contains object with the same value.

var arr1, arr2, result;

arr1 = [
  {"AGIMENDO Info Objekt 1": "00000000", "Beschreibung Kurz": "Test0", Währung: "200.00", __rowNum__: 1},
  {"AGIMENDO Info Objekt 1": "00000001", "Beschreibung Kurz": "Update1", Währung: "456.00", __rowNum__: 2},
  {"AGIMENDO Info Objekt 1": "00000002", "Beschreibung Kurz": "Test2", Währung: "12.00", __rowNum__: 3},
  {"AGIMENDO Info Objekt 1": "00000003", "Beschreibung Kurz": "Test3", Währung: "549153.00", __rowNum__: 4},
  {"AGIMENDO Info Objekt 1": "00000004", "Beschreibung Kurz": "Text", Währung: "1.05", __rowNum__: 5},
  {"AGIMENDO Info Objekt 1": "00000005", "Beschreibung Kurz": "13.08.11", Währung: "465.00", __rowNum__: 6},
  {"AGIMENDO Info Objekt 1": "00000006", "Beschreibung Kurz": "Test21", Währung: "4594.00", __rowNum__: 7}
];

arr2 = [
  {type: "number", value: "200.00"},
  {type: "number", value: "456.00"},
  {type: "number", value: "12.00"},
  {type: "number", value: "549153.00"},
  {type: "number", value: "1.05"},
  {type: "date", value: "13.08.11"},
  {type: "number", value: "465.00"}
       ]

result = arr1.map(obj=>{
  let copy = Object.assign({}, obj);
  Object.entries(obj).forEach(([key, val])=>{
    let type = arr2.find(el=>el.value === val);
    if (type){
      copy[key] = type;
    }
  })
  return copy;
})

console.log(result)

Upvotes: 1

evgeni fotia
evgeni fotia

Reputation: 4810

I am not sure if you want __rowNum__ to be in the out put or not

var arrA = [{'AGIMENDO Info Objekt 1': "00000000", 'Beschreibung Kurz': "Test0", 'Währung': "200.00", '__rowNum__': 1}, {'AGIMENDO Info Objekt 1': "00000001", 'Beschreibung Kurz': "Update1", 'Währung': "456.00", '__rowNum__': 2}];

var arrB = [{type: "number", value: "200.00"}, {type: "number", value: "456.00"}];

var resultat = arrA.map((obj, index)=>(    Object.assign(obj, {'Währung': {
     'Value': arrB[index].value,
     'Type': arrB[index].type
  }})
));

console.log(resultat);

Upvotes: 1

Related Questions