0

I have something like this:

val = val.split(".");
//val[0] is name
//val[1] is John

I have to build object like this:
{name: "John"}

So I do:

filterArray = "'{'"+val[0]+"':'"+val[1]+"'}'";

var t = JSON.parse(filterArray);

And when I do console.log(filterArray);
I want to get: Object { name: "John" }

But can't do it the right why, please help :)

George Kagan
  • 5,913
  • 8
  • 46
  • 50
felixRo
  • 187
  • 1
  • 1
  • 14

1 Answers1

0

This is how that should be done

val = val.split(".");
// Declaring an empty object
obj = {};
// Assingning a dynamic key and its value form you variable
obj[val[0]] = val[1];
console.log(obj) // Object { name: "John" }
S Vinesh
  • 529
  • 1
  • 4
  • 15
  • Though this is correct way, you should not answer basic questions like these. A comment is more than enough – Rajesh Nov 15 '16 at 11:40
  • No need for sorry. Just remember, soon you will get privilege to close a question. Use it instead of answering – Rajesh Nov 15 '16 at 11:42
  • Thank you for helping and Rajesh thank you for your basic answer. – felixRo Nov 15 '16 at 11:47