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

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

create an object array from another object accessing the key

问题

I have an array

  1. const allocatedItem = []

I have an array of objects

  1. const g = {
  2. "Galley1": {
  3. "id": "Box-1",
  4. "rollNo": "",
  5. "name": "Flasks",
  6. "bgColorCode": "#058af7"
  7. },
  8. // ... (other objects)
  9. }

I need to make allocated items arrays with the specified structure:

  1. const allocatedItem = [
  2. {
  3. "cartId": g.Galley1.name,
  4. "itemId": "1"
  5. },
  6. {
  7. "cartId": g.Galley2.name,
  8. "itemId": "2"
  9. },
  10. {
  11. "cartId": g.Galley3.name,
  12. "itemId": "2"
  13. }
  14. // ... (continue for Galley4, Galley5, Galley6, Galley9, etc.)
  15. ]

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

  1. const allocatedItem= []

i have an an array of objects

  1. const g={
  2. "Galley1": {
  3. "id": "Box-1",
  4. "rollNo": "",
  5. "name": "Flasks",
  6. "bgColorCode": "#058af7"
  7. },
  8. "Galley2": {
  9. "id": "Box-2",
  10. "rollNo": "",
  11. "name": "Perishable and Water Cart",
  12. "bgColorCode": "#f5f102"
  13. },
  14. "Galley3": {
  15. "id": "Box-3",
  16. "rollNo": "",
  17. "name": "Drystores and Cups",
  18. "bgColorCode": "#6e6e67"
  19. },
  20. "Galley4": {
  21. "id": "Box-5",
  22. "rollNo": "",
  23. "name": "Vaccum Flasks",
  24. "bgColorCode": "#c0c0c0"
  25. },
  26. "Galley11": {
  27. "id": "Box-2",
  28. "rollNo": "",
  29. "name": "Perishable and Water Cart",
  30. "bgColorCode": "#f5f102"
  31. },
  32. "Galley10": {
  33. "id": "Box-7",
  34. "rollNo": "",
  35. "name": "Flexi SMU",
  36. "bgColorCode": "#058af7"
  37. },
  38. "Galley9": {
  39. "id": "Box-6",
  40. "rollNo": "",
  41. "name": "ROB SMU",
  42. "bgColorCode": "#6e6e67"
  43. },
  44. "Galley8": {
  45. "id": "Box-8",
  46. "rollNo": "",
  47. "name": "Dessert Cart",
  48. "bgColorCode": "#c0c0c0"
  49. },
  50. "Galley7": {
  51. "id": "Box-9",
  52. "rollNo": "",
  53. "name": "Liquor",
  54. "bgColorCode": "#f5f102"
  55. },
  56. "Galley6": {
  57. "id": "Box-10",
  58. "rollNo": "",
  59. "name": "Meals Tray",
  60. "bgColorCode": "#009933"
  61. },
  62. "Galley5": {
  63. "id": "Box-11",
  64. "rollNo": "",
  65. "name": "Bottles",
  66. "bgColorCode": "#058af7"
  67. }
  68. }

i need to make allocated items arrays in such a way that it should be having objects in the structure

  1. const allocatedItem= [ {
  2. "cartId": g.Galley1.name,
  3. "itemId": "1"
  4. },
  5. {
  6. "cartId": g.Galley2.name,
  7. "itemId": "2"
  8. },
  9. {
  10. "cartId": g.Galley3.name,
  11. "itemId": "2"
  12. }
  13. ]

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.

  1. const g = {
  2. Galley1: {
  3. id: "Box-1",
  4. rollNo: "",
  5. name: "Flasks",
  6. bgColorCode: "#058af7"
  7. },
  8. // ... (other object entries)
  9. };
  10. const allocatedItem = [];
  11. let index = 1;
  12. for (let key in g) {
  13. let obj = {};
  14. obj.cartId = g[key].name;
  15. obj.itemId = index + "";
  16. allocatedItem.push(obj);
  17. index += 1;
  18. }
  19. 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 -->

  1. const g = {
  2. Galley1: {
  3. id: &quot;Box-1&quot;,
  4. rollNo: &quot;&quot;,
  5. name: &quot;Flasks&quot;,
  6. bgColorCode: &quot;#058af7&quot;
  7. },
  8. Galley2: {
  9. id: &quot;Box-2&quot;,
  10. rollNo: &quot;&quot;,
  11. name: &quot;Perishable and Water Cart&quot;,
  12. bgColorCode: &quot;#f5f102&quot;
  13. },
  14. Galley3: {
  15. id: &quot;Box-3&quot;,
  16. rollNo: &quot;&quot;,
  17. name: &quot;Drystores and Cups&quot;,
  18. bgColorCode: &quot;#6e6e67&quot;
  19. },
  20. Galley4: {
  21. id: &quot;Box-5&quot;,
  22. rollNo: &quot;&quot;,
  23. name: &quot;Vaccum Flasks&quot;,
  24. bgColorCode: &quot;#c0c0c0&quot;
  25. },
  26. Galley11: {
  27. id: &quot;Box-2&quot;,
  28. rollNo: &quot;&quot;,
  29. name: &quot;Perishable and Water Cart&quot;,
  30. bgColorCode: &quot;#f5f102&quot;
  31. },
  32. Galley10: {
  33. id: &quot;Box-7&quot;,
  34. rollNo: &quot;&quot;,
  35. name: &quot;Flexi SMU&quot;,
  36. bgColorCode: &quot;#058af7&quot;
  37. },
  38. Galley9: {
  39. id: &quot;Box-6&quot;,
  40. rollNo: &quot;&quot;,
  41. name: &quot;ROB SMU&quot;,
  42. bgColorCode: &quot;#6e6e67&quot;
  43. },
  44. Galley8: {
  45. id: &quot;Box-8&quot;,
  46. rollNo: &quot;&quot;,
  47. name: &quot;Dessert Cart&quot;,
  48. bgColorCode: &quot;#c0c0c0&quot;
  49. },
  50. Galley7: {
  51. id: &quot;Box-9&quot;,
  52. rollNo: &quot;&quot;,
  53. name: &quot;Liquor&quot;,
  54. bgColorCode: &quot;#f5f102&quot;
  55. },
  56. Galley6: {
  57. id: &quot;Box-10&quot;,
  58. rollNo: &quot;&quot;,
  59. name: &quot;Meals Tray&quot;,
  60. bgColorCode: &quot;#009933&quot;
  61. },
  62. Galley5: {
  63. id: &quot;Box-11&quot;,
  64. rollNo: &quot;&quot;,
  65. name: &quot;Bottles&quot;,
  66. bgColorCode: &quot;#058af7&quot;
  67. }
  68. };
  69. const allocatedItem = [];
  70. let index = 1;
  71. for (let key in g) {
  72. let obj = {};
  73. obj.cartId = g[key].name;
  74. obj.itemId = index + &quot;&quot;;
  75. allocatedItem.push(obj)
  76. index += 1;
  77. }
  78. 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:

确定