从另一个对象访问键创建一个对象数组。

huangapple go评论65阅读模式
英文:

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: &quot;Box-1&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Flasks&quot;,
    bgColorCode: &quot;#058af7&quot;
  },
  Galley2: {
    id: &quot;Box-2&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Perishable and Water Cart&quot;,
    bgColorCode: &quot;#f5f102&quot;
  },
  Galley3: {
    id: &quot;Box-3&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Drystores and Cups&quot;,
    bgColorCode: &quot;#6e6e67&quot;
  },
  Galley4: {
    id: &quot;Box-5&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Vaccum Flasks&quot;,
    bgColorCode: &quot;#c0c0c0&quot;
  },
  Galley11: {
    id: &quot;Box-2&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Perishable and Water Cart&quot;,
    bgColorCode: &quot;#f5f102&quot;
  },
  Galley10: {
    id: &quot;Box-7&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Flexi SMU&quot;,
    bgColorCode: &quot;#058af7&quot;
  },
  Galley9: {
    id: &quot;Box-6&quot;,
    rollNo: &quot;&quot;,
    name: &quot;ROB SMU&quot;,
    bgColorCode: &quot;#6e6e67&quot;
  },
  Galley8: {
    id: &quot;Box-8&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Dessert Cart&quot;,
    bgColorCode: &quot;#c0c0c0&quot;
  },
  Galley7: {
    id: &quot;Box-9&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Liquor&quot;,
    bgColorCode: &quot;#f5f102&quot;
  },
  Galley6: {
    id: &quot;Box-10&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Meals Tray&quot;,
    bgColorCode: &quot;#009933&quot;
  },
  Galley5: {
    id: &quot;Box-11&quot;,
    rollNo: &quot;&quot;,
    name: &quot;Bottles&quot;,
    bgColorCode: &quot;#058af7&quot;
  }
};


const allocatedItem = [];
let index = 1;

for (let key in g) {
  let obj = {};
  obj.cartId = g[key].name;
  obj.itemId = index + &quot;&quot;;
  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.

huangapple
  • 本文由 发表于 2023年5月7日 18:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定