繁体   English   中英

重新排列 javascript object 结构

[英]Rearrange the javascript object structure

我想重新安排一个 object 的结构,从昨天开始就卡住了,所以我需要你的重要帮助。

目前,结构如下:

var data = [{
    id: 14,
    language: "english",
    title: "I am a new article",
    bodyText: "Article Content",
    lang: "eng",
    keywords: ["key1", "key2"]
  },
  {
    id: 1,
    language: "greeks",
    title: "Ειμαι ενα καινουρειο αρθρο",
    bodyText: "Κυριο μερος Αρθρου",
    lang: "gr",
    keywords: ["key1", "key2"]
  },
  {
    id: 1,
    language: "espanol",
    title: "Soy un nuevo articulo",
    bodyText: "Soy un nuevo articulo",
    lang: "es",
    keywords: ["key1", "key2"]
  },

]

我想将结构重新排列为以下格式:

var data = [{
  id: 1,
  language: {
    es: {
      title: "Spanish Article",
      bodyText: "Content in Spanish"
    },
    gr: {

      title: "Greek Article",
      bodyText: "Content in Grecce"
    }
  },
  id: 2,
  language: {
    en: {
      title: "English Article",
      bodyText: "Content in English"
    }
  }
}];

我编写了以下代码来完成任务,但没有运气。

var arr = [];
let result = data.reduce(function(c, v) {
  console.log(v.id);
  /*   console.log(c);
   */
  c[v.id] = c[v.id] || {};
  c[v.id][v.lang] = c[v.id][v.lang] || {
    title: v.title,
    bodyText: v.bodyText,
    keywords: v.keywords
  };
  arr.push(c)
  return c;
}, {});
console.log(arr);

我得到一个 object,如下所示:

[{
id:1,
es:{
title:"Spanish Article",
bodyText:"Content in Spanish"
},
gr:{
title:"Greek Article",
bodyText:"Content in Grecce"
},
id:2,
en:{
title:"English Article",
bodyText:"Content in English"
}
}]

欢迎任何建议,提前感谢社区!

您的目标数据 model 似乎有点次优,因为您有一个具有唯一 id 的数组,它可以作为 Object 以 id 作为键,但您可以摆脱您的数据 Z20F35E630DAF44DFA9C4C3:

 var data = [ { id: 14, language: "english", title: "I am a new article", bodyText: "Article Content", lang: "eng", keywords: ["key1", "key2"] }, { id: 1, language: "greeks", title: "Ειμαι ενα καινουρειο αρθρο", bodyText: "Κυριο μερος Αρθρου", lang: "gr", keywords: ["key1", "key2"] }, { id: 1, language: "espanol", title: "Soy un nuevo articulo", bodyText: "Soy un nuevo articulo", lang: "es", keywords: ["key1", "key2"] } ]; console.log(data.reduce(function(result, entry) { var id_index = result.map(function(e) { return e.id; }).indexOf(entry.id); var id_element; if (id_index === -1) { id_element = {id: entry.id, language: {}}; } else { id_element = result[id_index]; } id_element.language[entry.lang] = { title: entry.title, bodyText: entry.bodyText }; if (id_index === -1) { result.push(id_element); } return result; }, []))

您可以解构属性并稍后使用 hash 表中的值。

主要部分是采用具有language属性的初始 object。

 var data = [{ id: 14, language: "english", title: "I am a new article", bodyText: "Article Content", lang: "eng", keywords: ["key1", "key2"] }, { id: 1, language: "greeks", title: "Ειμαι ενα καινουρειο αρθρο", bodyText: "Κυριο μερος Αρθρου", lang: "gr", keywords: ["key1", "key2"] }, { id: 1, language: "espanol", title: "Soy un nuevo articulo", bodyText: "Soy un nuevo articulo", lang: "es", keywords: ["key1", "key2"] }], result = data.reduce((c, { id, lang, title, bodyText, keywords }) => { c[id] = c[id] || { id, language: {} }; c[id].language[lang] = { title, bodyText, keywords }; return c; }, {}), array = Object.values(result); console.log(array);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM