-1

enter image description here

I have array like above. I want to input {"temperature":{"work":30,"home":24}} object to the first of array.

So array should start with:

0 : {title : "tptp", {"temperature":{"work":30,"home":24}}, lastview:"12-12 21:2"}

My code is

console.log("below is home");
console.log(this.home);
console.log(this.home[0].push({"temperature": {"work":30,"home":24}}));

But I have error TypeError: this.home[0].push is not a function.

Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Pd.Jung
  • 113
  • 1
  • 3
  • 13

3 Answers3

0

The expected output you posted isn't possible, i.e.

{title : "tptp", {"temperature":{"work":30,"home":24}}, lastview:"12-12 21:2"}

what you probably want is

{title : "tptp", "temperature":{"work":30,"home":24}, lastview:"12-12 21:2"}

which you can achieve with

Object.assign(this.home[0], {"temperature": {"work":30,"home":24}})
ic3b3rg
  • 14,629
  • 4
  • 30
  • 53
0

push function works with Array, not with Object and

{title : "tptp", {"temperature":{"work":30,"home":24}}, lastview:"12-12 21:2"} // Ivalid Json

You have to interduce new key temperature according to your requirement.

console.log("below is home");
console.log(this.home);
this.home[0]["temperature"] = {"work":30,"home":24};
console.log( this.home[0] );

Output will be

[
  {
    "title": "tptp",
    "lastview": "12-12 21:2",
    "temperature": {
      "work": 30,
      "home": 24
    }
  },
  {
    "title": "gngn",
    "lastview": "12-12 19:29"
  }
]
Vinesh Goyal
  • 607
  • 5
  • 8
-1

What you can do is ,

this.home[0].put("temperature": {"work":30,"home":24});
Pratik Rathi
  • 76
  • 1
  • 9