In response i get this king of data, here in data object there is various items but there key are kind of common so how can i get that data and add in my categories list.
{
"99": "Venues",
"100": "Party Supplies",
"101": "Entertainment",
"102": "Desserts",
"103": "Catering"
}
here, how i tried to set the data in my list.
const dataItem = data.data;
const newList = [];
newList.push({item: dataItem[99]});
newList.push({item: dataItem[100]});
newList.push({item: dataItem[101]});
setList(newList);
but when i don't have the proper name of key then how can i add it to my list? ex. sometimes i get the key like...
{
"S99": "Venues",
"SDF100": "Party Supplies",
"CF101": "Entertainment",
"VF102": "Desserts",
"CFCV103": "Catering"
}
So, how can i set the data in list without knowing the item key.
CodePudding user response:
You can get key and value both by using following code
for (var key in data.data) {
console.log(key); // This will return a key, in your case it will be S99, SDF100...
console.log(JSON_Obj[key]); // This will return value for that key.
newList.push({item: data.data[key]});
}
from this you can add values in your list
CodePudding user response:
let data = {
"S99": "Venues",
"SDF100": "Party Supplies",
"CF101": "Entertainment",
"VF102": "Desserts",
"CFCV103": "Catering"
}
Const list = Object.values(data);
console.log(list);
// print (5) ['Venues', 'Party Supplies', 'Entertainment', 'Desserts', 'Catering']