将对象的数组组合到JavaScript中的一个对象

发布于 2025-02-10 06:01:01 字数 564 浏览 2 评论 0原文

let myCurrentData = [
  {
    _id: "D01",
    name: "Lunetts",
    profession: "shop",
  },
  {
    _id: "D02",
    name: "Glasses",
    profession: "keeper",
  },
  {
    _id: "D03",
    name: "Auros",
    profession: "UiiSii",
  },
];

上面是我的mycurrentdata,我想将此数组转换为最终对象如下

 let myFinalData = {
 D01: "Lunetts",
  D02: "Glasses",
  D03: "Auros"
}

我只想使用_id和名称的值

let myCurrentData = [
  {
    _id: "D01",
    name: "Lunetts",
    profession: "shop",
  },
  {
    _id: "D02",
    name: "Glasses",
    profession: "keeper",
  },
  {
    _id: "D03",
    name: "Auros",
    profession: "UiiSii",
  },
];

Above is my myCurrentData, I want to convert this array into a final object like the following

 let myFinalData = {
 D01: "Lunetts",
  D02: "Glasses",
  D03: "Auros"
}

i just want to use the values of _id and name

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

追星践月 2025-02-17 06:01:01
const myCurrentData = [
  {
    _id: "D01",
    name: "Lunetts",
    profession: "shop",
  },
  {
    _id: "D02",
    name: "Glasses",
    profession: "keeper",
  },
  {
    _id: "D03",
    name: "Auros",
    profession: "UiiSii",
  },
];

const finalObject = myCurrentData
  .map((eachObject) => {
    const { profession, name, _id } = eachObject;
    return {
      [_id]: name,
    };
  })
  .reduce((prev, current) => {
    return {
      ...prev,
      ...current,
    };
  }, {});

console.log("finalObject is", finalObject);

希望它有效!

const myCurrentData = [
  {
    _id: "D01",
    name: "Lunetts",
    profession: "shop",
  },
  {
    _id: "D02",
    name: "Glasses",
    profession: "keeper",
  },
  {
    _id: "D03",
    name: "Auros",
    profession: "UiiSii",
  },
];

const finalObject = myCurrentData
  .map((eachObject) => {
    const { profession, name, _id } = eachObject;
    return {
      [_id]: name,
    };
  })
  .reduce((prev, current) => {
    return {
      ...prev,
      ...current,
    };
  }, {});

console.log("finalObject is", finalObject);

Hope it works!

城歌 2025-02-17 06:01:01
const data=[{_id:"D01",name:"Lunetts",profession:"shop"},{_id:"D02",name:"Glasses",profession:"keeper"},{_id:"D03",name:"Auros",profession:"UiiSii"}];

const out = {};

for (const obj of data) {
  out[obj._id] = obj.name;
}

console.log(out);

Use a simple loop to create a new object.

const data=[{_id:"D01",name:"Lunetts",profession:"shop"},{_id:"D02",name:"Glasses",profession:"keeper"},{_id:"D03",name:"Auros",profession:"UiiSii"}];

const out = {};

for (const obj of data) {
  out[obj._id] = obj.name;
}

console.log(out);

老娘不死你永远是小三 2025-02-17 06:01:01

您可以使用降低并从中返回对象来实现此目的:

const myObj=  myCurrentData.reduce((acc,curr)=> {
    return {...acc, [curr._id]:curr.name}
  
}, {})

You can achieve this using reduce and returning an object from it:

const myObj=  myCurrentData.reduce((acc,curr)=> {
    return {...acc, [curr._id]:curr.name}
  
}, {})
高速公鹿 2025-02-17 06:01:01

您可以使用对象的优势。进入方法与foreach循环相结合以获取属性名称和值并将其添加到先前声明的myFinalData对象中。像这样的东西可能是您想要的。希望它有帮助,芽。

const myCurrentData = [
  {
    _id: "D01",
    name: "Lunetts",
    profession: "shop",
  },
  {
    _id: "D02",
    name: "Glasses",
    profession: "keeper",
  },
  {
    _id: "D03",
    name: "Auros",
    profession: "UiiSii",
  },
];


 const myFinalData = {}
Object.entries(myCurrentData).forEach(([k,v]) => { myFinalData[v['_id']] = v['name']})

console.log(myFinalData);

You could take advantages of the Object.entries method combined with a forEach loop to get the properties name and values and add it to your previously declare myFinalData Object. Something like this could be what you are looking for. Hope it helps, bud.

const myCurrentData = [
  {
    _id: "D01",
    name: "Lunetts",
    profession: "shop",
  },
  {
    _id: "D02",
    name: "Glasses",
    profession: "keeper",
  },
  {
    _id: "D03",
    name: "Auros",
    profession: "UiiSii",
  },
];


 const myFinalData = {}
Object.entries(myCurrentData).forEach(([k,v]) => { myFinalData[v['_id']] = v['name']})

console.log(myFinalData);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文