I have an object and I (to create a single function) wanted to see if the properties could be "called". I found the corresponding code, two properties work (itemName and inputId), and the rest (itemId and itemParentId) do not. That is, an alert with a message and text is displayed for the "working" properties, while an alert also displayed for the "non-working" properties, but it is absolutely empty. There were no errors in the console. Please tell me what is wrong?

 var listCategories = { data: [ { itemId: 1, itemName: "пункт №1", itemParentId: 0, inputId: 'n_' + 1 }, { itemId: 2, itemName: "пункт №1.1", itemParentId: 1, inputId: 'n_' + 2 }, { itemId: 3, itemName: "пункт №1.2", itemParentId: 1, inputId: 'n_' + 3 }, { itemId: 4, itemName: "пункт №2", itemParentId: 0, inputId: 'n_' + 4 }, { itemId: 5, itemName: "пункт №3", itemParentId: 0, inputId: 'n_' + 5 }, { itemId: 6, itemName: "пункт №3.1", itemParentId: 5, inputId: 'n_' + 6 }, { itemId: 7, itemName: "пункт №3.2", itemParentId: 5, inputId: 'n_' + 7 }, { itemId: 8, itemName: "пункт №3.3", itemParentId: 5, inputId: 'n_' + 8 }, { itemId: 9, itemName: "пункт №4", itemParentId: 0, inputId: 'n_' + 9 }, { itemId: 10, itemName: "пункт №5", itemParentId: 0, inputId: 'n_' + 10 }, { itemId: 11, itemName: "пункт №5.1", itemParentId: 10, inputId: 'n_' + 11 } ] }; function showProps(obj, objName) { var result = ""; for (var i in obj) { if (obj.hasOwnProperty(i)) { result += objName + "." + i + " = " + obj[i] + "\n"; } } alert(result); } showProps(listCategories.data[0].itemId, "listCategories.data[0].itemId");//это не работает showProps(listCategories.data[0].itemName, "listCategories.data[0].itemName");//это работает showProps(listCategories.data[0].itemParentId, "listCategories.data[0].itemParentId");//это не работает showProps(listCategories.data[0].inputId, "listCategories.data[0].inputId");//это работает 

  • 2
    Excuse me, do you want to display any of your own properties of the Number object? Do you understand what the showProps function showProps ? - Dmitriy Simushev
  • @DmitriySimushev Yes, I sort of understand it less ... And it seems your comment helped me understand the problem, thank you: "D - Mark_8

1 answer 1

 var listCategories = { data: [ { itemId: 1, itemName: "пункт №1", itemParentId: 0, inputId: 'n_' + 1 }, { itemId: 2, itemName: "пункт №1.1", itemParentId: 1, inputId: 'n_' + 2 } ] }, l = listCategories.data.length while (l--) { showProps(listCategories.data[l]) }; function showProps(obj) { for (var i in obj) { if (obj.hasOwnProperty(i)) { console.log(obj[i]); } } }