5

Excuse me for this simple problem - but I seem to miss something obvious.Any pointer would be a great help.

I have a JSON like

var  whatever = [{           
"key1" : { "text" : "text1","group"  : "1" },
"key2" : { "text" : "text2","group"  : "2" },
"key3" : { "text" : "text3","group"  : "3" }
}];

I am trying to add another object(at start preferably) - but just couldn't get it to work.

var str = '{"text":"text0","group":"0"}';
var obj = JSON.parse(str);
whatever[0].put("key0",obj);

Getting the below error:

Uncaught TypeError: whatever[0].put is not a function

fiddle

4
  • Object properties do not have an order, so adding it "at the start" makes no sense. Commented Sep 22, 2017 at 8:31
  • just a question, why an array? Commented Sep 22, 2017 at 8:31
  • The error was pretty clear. Don't take this as being rude, but you should really try harder to understand what the interpreter/compiler tells you. As for javascript opening a console writing 'obj.' shows you completion that are really helpful as a first source of documentation Commented Sep 22, 2017 at 8:35
  • @NinaScholz I was trying to order the entries but after going through the replies - I realize that this is not the best way to go about it. Commented Sep 22, 2017 at 9:15

2 Answers 2

3

There is no put function on the object. Use property instead of it. When you want to assign to a property which does not exist, it creates a new one and assigns the value to it.

whatever[0]["key0"] = obj;

What is related to at start preferably, there is no order for object properties. It is a wrong statement. If you want ordering try to think from the view of array of objects instead of array of object, which contains objects.

Code examples

const whatever = [{           
   "key1" : { "text" : "text1","group"  : "1" },
   "key2" : { "text" : "text2","group"  : "2" },
   "key3" : { "text" : "text3","group"  : "3" }
}];

const str = '{ "text" : "text0", "group" : "0" }';
const obj = JSON.parse(str);

whatever[0]["key0"] = obj;

console.log(whatever);

Or use Object#assign

const whatever = [{           
   "key1" : { "text" : "text1","group"  : "1" },
   "key2" : { "text" : "text2","group"  : "2" },
   "key3" : { "text" : "text3","group"  : "3" }
}];

const str = '{ "text" : "text0", "group" : "0" }';
const obj = JSON.parse(str);

Object.assign(whatever[0], { key0: obj }) // this will also change the object

console.log(whatever);

My suggestion is to use an array of objects, if you want something with order.

const whatever = [
   { "text" : "text1","group"  : "1" },
   { "text" : "text2","group"  : "2" },
   { "text" : "text3","group"  : "3" }
];

const str = '{ "text" : "text0", "group" : "0" }';
const obj = JSON.parse(str);

// Add to the start
whatever.unshift(obj);
console.log(whatever);

// Add to the end
whatever.push(obj);
console.log(whatever);

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

4 Comments

Technically wrong, "at start preferably" yours will add it to the end. unshift() to append to the front of an array should be used.
@Adriani6 Didn't look at that part. Edited
Perfect, Now it's a complete answer.
Perfect explanation.I stand corrected on the way how to order.I will modify it accordingly straight away.Thanks a lot.
1

maybe you want something like this

var  whatever = [{           
"key1" : { "text" : "text1","group"  : "1" },
"key2" : { "text" : "text2","group"  : "2" },
"key3" : { "text" : "text3","group"  : "3" }
}];

Object.assign(whatever[0], {key4 : { "text" : "text4","group"  : "4" }});

console.log(whatever);

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.