英文:
create an object array from another object accessing the key
问题
I have an array
const allocatedItem = []
I have an array of objects
const g = {
"Galley1": {
"id": "Box-1",
"rollNo": "",
"name": "Flasks",
"bgColorCode": "#058af7"
},
// ... (other objects)
}
I need to make allocated items arrays with the specified structure:
const allocatedItem = [
{
"cartId": g.Galley1.name,
"itemId": "1"
},
{
"cartId": g.Galley2.name,
"itemId": "2"
},
{
"cartId": g.Galley3.name,
"itemId": "2"
}
// ... (continue for Galley4, Galley5, Galley6, Galley9, etc.)
]
The objects should be added until all the Galley objects are mapped in cartId, and the itemId should be auto-incremented. Is there a way to do this rather than accessing it from the object like I did for cartId?
英文:
I have an array
const allocatedItem= []
i have an an array of objects
const g={
"Galley1": {
"id": "Box-1",
"rollNo": "",
"name": "Flasks",
"bgColorCode": "#058af7"
},
"Galley2": {
"id": "Box-2",
"rollNo": "",
"name": "Perishable and Water Cart",
"bgColorCode": "#f5f102"
},
"Galley3": {
"id": "Box-3",
"rollNo": "",
"name": "Drystores and Cups",
"bgColorCode": "#6e6e67"
},
"Galley4": {
"id": "Box-5",
"rollNo": "",
"name": "Vaccum Flasks",
"bgColorCode": "#c0c0c0"
},
"Galley11": {
"id": "Box-2",
"rollNo": "",
"name": "Perishable and Water Cart",
"bgColorCode": "#f5f102"
},
"Galley10": {
"id": "Box-7",
"rollNo": "",
"name": "Flexi SMU",
"bgColorCode": "#058af7"
},
"Galley9": {
"id": "Box-6",
"rollNo": "",
"name": "ROB SMU",
"bgColorCode": "#6e6e67"
},
"Galley8": {
"id": "Box-8",
"rollNo": "",
"name": "Dessert Cart",
"bgColorCode": "#c0c0c0"
},
"Galley7": {
"id": "Box-9",
"rollNo": "",
"name": "Liquor",
"bgColorCode": "#f5f102"
},
"Galley6": {
"id": "Box-10",
"rollNo": "",
"name": "Meals Tray",
"bgColorCode": "#009933"
},
"Galley5": {
"id": "Box-11",
"rollNo": "",
"name": "Bottles",
"bgColorCode": "#058af7"
}
}
i need to make allocated items arrays in such a way that it should be having objects in the structure
const allocatedItem= [ {
"cartId": g.Galley1.name,
"itemId": "1"
},
{
"cartId": g.Galley2.name,
"itemId": "2"
},
{
"cartId": g.Galley3.name,
"itemId": "2"
}
]
The objects should be added till the number of galley objects are mapped in cartId ie Galley4,5,6 ,9etc. The itemId should be auto incremented.Is there a way to do this rather than accessing it from the object like i did for cartId.
答案1
得分: 0
You can use a for
loop to iterate over the g
object, and achieve the desired result.
const g = {
Galley1: {
id: "Box-1",
rollNo: "",
name: "Flasks",
bgColorCode: "#058af7"
},
// ... (other object entries)
};
const allocatedItem = [];
let index = 1;
for (let key in g) {
let obj = {};
obj.cartId = g[key].name;
obj.itemId = index + "";
allocatedItem.push(obj);
index += 1;
}
console.log(allocatedItem);
The for loop iterates over the keys of the g
object. We also declared the index
variable with a value of 1 to keep track of the itemId
. Inside the for loop, we declare an empty object and add the required key-values to it. Then, we append the object to the array.
英文:
You can use a for
loop to iterate over the g
object, and achieve desired result.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const g = {
Galley1: {
id: "Box-1",
rollNo: "",
name: "Flasks",
bgColorCode: "#058af7"
},
Galley2: {
id: "Box-2",
rollNo: "",
name: "Perishable and Water Cart",
bgColorCode: "#f5f102"
},
Galley3: {
id: "Box-3",
rollNo: "",
name: "Drystores and Cups",
bgColorCode: "#6e6e67"
},
Galley4: {
id: "Box-5",
rollNo: "",
name: "Vaccum Flasks",
bgColorCode: "#c0c0c0"
},
Galley11: {
id: "Box-2",
rollNo: "",
name: "Perishable and Water Cart",
bgColorCode: "#f5f102"
},
Galley10: {
id: "Box-7",
rollNo: "",
name: "Flexi SMU",
bgColorCode: "#058af7"
},
Galley9: {
id: "Box-6",
rollNo: "",
name: "ROB SMU",
bgColorCode: "#6e6e67"
},
Galley8: {
id: "Box-8",
rollNo: "",
name: "Dessert Cart",
bgColorCode: "#c0c0c0"
},
Galley7: {
id: "Box-9",
rollNo: "",
name: "Liquor",
bgColorCode: "#f5f102"
},
Galley6: {
id: "Box-10",
rollNo: "",
name: "Meals Tray",
bgColorCode: "#009933"
},
Galley5: {
id: "Box-11",
rollNo: "",
name: "Bottles",
bgColorCode: "#058af7"
}
};
const allocatedItem = [];
let index = 1;
for (let key in g) {
let obj = {};
obj.cartId = g[key].name;
obj.itemId = index + "";
allocatedItem.push(obj)
index += 1;
}
console.log(allocatedItem)
<!-- end snippet -->
The for loop iterates over the keys of the g
object. We also declared the index
variable, with value of 1, to keep track of the itemId
. Inside the for loop, we declare an empty object, and add the required key-values to it. Then, we append the object to the array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论