Get variable value from an object - Jquery

javascript jquery object

52 观看

2回复

55 作者的声誉

This question already has an answer here:

I am trying to get variable value from an object in jquery but its returning undefined.

console.log(itemName.name);

Returns: unidentified

Code:

 console.log(itemName);

Object:

enter image description here

I need the value of the name variable, don't know the exact syntax to grab it from the object.

作者: ALi Akbar 的来源 发布者: 2017 年 12 月 27 日

回应 2


3

58712 作者的声誉

决定

You should get the first child first then get the name like :

itemName["-L0uwNE54j88Gu15iuRD"].name

If the first child name is unknown you could use Object.keys() to get it:

var first_child_name = Object.keys(itemName)[0];
itemName[first_child_name].name

var itemName = {
  "-L0uwNE54j88Gu15iuRD": {
    id: 1,
    name: "Cakes"
  }
}

console.log(itemName["-L0uwNE54j88Gu15iuRD"].name);
console.log(itemName[Object.keys(itemName)[0]].name);

作者: Zakaria Acharki 发布者: 2017 年 12 月 27 日

0

1 作者的声誉

This should solve the purpose:

itemName.-L0uwNE54j88Gu15iuRD.name

作者: kapil upadhyay 发布者: 2017 年 12 月 27 日
32x32