0

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?

7
  • 4
    please add data in text form, not as images. - and what you have tried. Commented Oct 22, 2018 at 10:29
  • @sonja Why don't you modify the original array in the first place rather than creating and then merging. Commented Oct 22, 2018 at 10:30
  • @Vishnudev The first array gets created by an excel upload (using SheetJS). The library gives a function "sheet_to_json" which creates the first array from an uploaded excel file. So unfortunately I can't add anything to that creation.. Commented Oct 22, 2018 at 10:35
  • @sonja Just create a copy variable of the output from sheetjs and do it Commented Oct 22, 2018 at 10:45
  • Do these two arrays have the same order of elements? Commented Oct 22, 2018 at 10:46

3 Answers 3

1

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

thanks! But unfortunately that's not 100% dynamic. I should get the column name "Währung" dynamically since it might be a different name or a different type.. Any ideas how that would work? other than that, it's perfect..
@sonja you mean that "Währung" in the first array may have a different name?
@evgenifotia array with types contains also date and it matches column 'Beschreibung Kurz'
Yes, my JSON Model was only an example. It's an excel upload so it may be any name or it may not exist..!
0

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"))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.