2

Given:

var result1 = [{'p1':'v1'}];  
var result2 = [{'p2':'v2'}];  
var array1 = [{'p3':'v3'},{'p4':'v4'}];  

Rules:
If array has one property, add property to main array to return.
If array has multiple properties, add a label and keep array as is.

https://jsfiddle.net/3unx0hsa/5/

function mergeJson(data) {  
    let newarray1 = [];

    for (let index = 0; index < resultsArray.length; index++) {
        let element = resultsArray[index][0];

        if (element.length === 1) {            
            newarray1.push(element);
        }

        if (element.length > 1) {
            var x = `{data${index}: ${element}`;
            newarray1.push(x);
        }
    }
}

Illustration: enter image description here

3
  • 3
    You asked about merging json objects, but this seems to be more about a question of merging arrays that happen to contain objects. Is that right? Commented Jul 16, 2018 at 21:25
  • 1
    benalman.com/news/2010/03/theres-no-such-thing-as-a-json Commented Jul 16, 2018 at 21:25
  • Also, its not clear what this means If array has multiple properties, add a label and keep array as is. What do you mean by adding a label, and where in the data structure does this label expect to be? Commented Jul 16, 2018 at 21:28

3 Answers 3

3

Template string literal creates a string. You need an object literal instead

var x = {[`data${index}`]: element};

var result1 = [{'p1': 'v1'}];
var result2 = [{'p2': 'v2'}];
var array1 = [{'p3': 'v3'}, {'p4': 'v4'}];

let x = mergeJson([result1, result2, array1]);
console.log(x);

function mergeJson(resultsArray) {
  let newarray1 = [];

  for (let index = 0; index < resultsArray.length; index++) {
    let element = resultsArray[index];

    if (element.length === 1) {
      newarray1.push(element[0]);
    }

    if (element.length > 1) {
      var x = {[`data${index}`]: element};
      newarray1.push(x);
    }

  }

  return newarray1;
}

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

Comments

1

After this line: var x = `{data${index}: ${element}`;, the value for x is a string. That is what you are seeing in you output. Change that line to something like this:

var x = {`{data${index}`: element};

That should give you the result you're expecting.

1 Comment

That's invalid syntax. If you try to use an expression as the key in an object literal it needs to be inside [].
1

Using that type of function you will be able to display Json values

//JSON = objects and you have to call them for examle:

var result1 = [{'p1':'v1'}];  
var result2 = [{'p2':'v2'}];  
var array1 = result1.concat(result2);
for (i in array1){
 array1[i];
for(x in array1[i]){
document.getElementById("test").innerHTML += x+" - "+array1[i][x] +" <br>";
}
}
<div id="test"></div>

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.