Instead of using forEach, you can use the map to get the desired result
const newData = {
id: 111,
name: "ewfwef",
description: "Hello",
};
const data = Object.keys(newData).map((key) => ({ key, value: newData[key] }));
console.log(data);
You can add two keys in two ways:
1) If you don't want to change the resulting array then you can push elements in the array
const newData = {
id: 111,
name: "ewfwef",
description: "Hello",
};
const data = Object.keys(newData).map((key) => ({ key, value: newData[key] }));
data.push({ key: "day", value: "monday" });
data.push({ key: "week", value: "7th" });
console.log(data);
2) Or you can both key-value in the source object
const newData = {
id: 111,
name: "ewfwef",
description: "Hello",
day: "monday",
week: "7th",
};
const data = Object.keys(newData).map((key) => ({ key, value: newData[key] }));
console.log(data);
"item"instead of theitemvariable's value.Object.keys(newData).map(item => [{item, newData[item]}])const data = Object.entries(newData).map(([key, value]) => ({ key, value }));