3

I have an array with multiple objects.So how to make a new object by splitting the objects.

Here is how i am doing manually. How can I achieve it dynamically.

var a ='["{\\"Transfer_Notes__c\\":{\\"filterType\\":\\"text\\",\\"type\\":\\"contains\\",\\"filter\\":\\"abc\\"}}","{\\"IQ_Score__c\\":{\\"filterType\\":\\"number\\",\\"type\\":\\"equals\\",\\"filter\\":null,\\"filterTo\\":null}}"]';
var c = {};
c= JSON.parse(a);
const obj = Object.assign({}, JSON.parse(c[0]),JSON.parse(c[1]));


console.log(obj)

I tried several ways. Please help!

7
  • 2
    You have double-encoded JSON. Your parsed array contains JSON strings, not objects Commented Aug 11, 2020 at 15:06
  • Try using a loop (for const item of c) { ... // Your code here} Commented Aug 11, 2020 at 15:06
  • @PatrickRoberts That's why you see multiple calls to JSON.parse() What a I missing, how does your comment have two upvotes? Did you run the code? Commented Aug 11, 2020 at 15:07
  • 1
    That doesn't make it anymore sensible @JuanMendes Commented Aug 11, 2020 at 15:07
  • @Liam Sometimes you don't have control over what you're given Commented Aug 11, 2020 at 15:08

3 Answers 3

4

var a ='["{\\"Transfer_Notes__c\\":{\\"filterType\\":\\"text\\",\\"type\\":\\"contains\\",\\"filter\\":\\"abc\\"}}","{\\"IQ_Score__c\\":{\\"filterType\\":\\"number\\",\\"type\\":\\"equals\\",\\"filter\\":null,\\"filterTo\\":null}}"]';
var c = JSON.parse(a);
const obj = c.reduce((obj, c) => Object.assign(obj, JSON.parse(c)), {});

console.log(obj)

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

Comments

1

You could use a reviver function with JSON.parse() to specify that the array values should also be parsed. Then you can spread the parsed objects from the array into a resulting object using Object.assign():

var a ='["{\\"Transfer_Notes__c\\":{\\"filterType\\":\\"text\\",\\"type\\":\\"contains\\",\\"filter\\":\\"abc\\"}}","{\\"IQ_Score__c\\":{\\"filterType\\":\\"number\\",\\"type\\":\\"equals\\",\\"filter\\":null,\\"filterTo\\":null}}"]';

const parsed = JSON.parse(a, (key, val) => key ? JSON.parse(val) : val);
const c = Object.assign({}, ...parsed);
console.log(c);

Comments

-1

var a ='["{\\"Transfer_Notes__c\\":{\\"filterType\\":\\"text\\",\\"type\\":\\"contains\\",\\"filter\\":\\"abc\\"}}","{\\"IQ_Score__c\\":{\\"filterType\\":\\"number\\",\\"type\\":\\"equals\\",\\"filter\\":null,\\"filterTo\\":null}}"]';
var c = {};
c= JSON.parse(a);
const A ={};
var obj = {};
for(var i=0;i<c.length;i++){
  obj = Object.assign(A, JSON.parse(c[i]),);
}



console.log(obj)

Please suggest if there is any alternative way; Suggest me if i am doing wrong

5 Comments

Almost ideal, but you have logic mistake - you assign each string for new empty object, and final result contains only last string. Fix that - obj = Object.assign(obj, JSON.parse(c[i])); and your code will work
You can see from the output that you're only getting a single property, that's because you need to write to the same object when calling Object.assign. Right now, you're overwriting the object with every write
@SFKID This should be part of your question, not an answer. Please do not post an answer when you are actually asking a question, unless you know for sure that your answer is correct. stackoverflow.blog/2011/07/01/…
@JuanMendes Check it now.
@SFKID Please don't ignore my comment and then ask me for my help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.