【问题标题】:restructure json object using javascript使用 javascript 重构 json 对象
【发布时间】:2021-08-05 18:54:45
【问题描述】:

我有这个对象:

[
    {
        "_token": "lRM32nH7KAnt2xdDkUJBJYniNnANJVhG20BGnjHE",
        "academic[2][id]": "-1",
        "title[2][name]": "Test Title",
        "from_date[2]": "2021-05-16",
        "to_date[2]": "2021-05-17",
        "institute[2]": "Titletest title test title ",
        "title[3][name]": "Test TitleTest Title",
        "from_date[3]": "2021-05-17",
        "to_date[3]": "2021-05-18",
        "institute[3]": "test title test title test title test title "
    }
]

我想将其重组为:

[
  {"title": "Test Title", "from_date": "2021-05-17", "to_date": "2021-05-18","institute":"Title"},
  {"title": "Test TitleTest Title", "from_date": "2021-05-17", "to_date": "2021-05-18","institute":"Title"},
  {"title": "Test Title", "from_date": "2021-05-17", "to_date": "2021-05-18","institute":"Title"},
]

如何使用 javascript 做到这一点?或任何使用 javascript 的简单方法?

编辑: 到目前为止我所做的是:

 const data = new FormData(document.querySelector('#academic-form'));
 const result = [Object.fromEntries(data.entries())][0];

 const academics = [];
 for(var key in result){
   // console.log('key: ' + key);
   console.log('title: ' + result[key]);
   console.log(result[i]);
   academics.push({
       //push values in academics array. 
   });
    
 }

【问题讨论】:

  • 你尝试过什么代码?您应该将其添加到您的问题中。
  • 抱歉现在在我的问题中添加它。
  • @Andy 立即查看。
  • 只使用const result = data.map(item => ({ title: item['title[2][name]'], <othervalues> })); 怎么样,然后将<othervalues> 替换为附加属性(类似于标题)?
  • title[2][name] 仅用于演示,有多个值,范围从 2 到 10 或 15。在这种情况下该怎么办?

标签: javascript laravel javascript-objects laravel-8


【解决方案1】:

假设您按对象键中的数字进行分组*,您可以使用正则表达式将对象键分解为标签和数字,并在对象键/值对上使用 reduce 创建新对象使用该数字作为新密钥。然后,您可以使用 Object.values 从该对象创建一个数组。

*请注意,此输出仅生成两个对象,而不是预期输出中指示的三个对象。

const arr = [{
  "_token": "lRM32nH7KAnt2xdDkUJBJYniNnANJVhG20BGnjHE",
  "academic[2][id]": "-1",
  "title[2][name]": "Test Title",
  "from_date[2]": "202 1-05-16",
  "to_date[2]": "2021-05-17",
  "institute[2]": "Titletest title test title ",
  "title[3][name]": "Test TitleTest Title",
  "from_date[3]": "2021-05-17",
  "to_date[3]": "2021-05-18",
  "institute[3]": "test title test title test title test title "
}];

const regex = /(title|from_date|to_date|institute)(\[\d\])/;

// Iterate over the object grabbing the key and value
const out = Object.entries(arr[0]).reduce((acc, [key, value]) => {

  // Create a match array using the regex on the key
  const match = key.match(regex);

  // If there is a match...
  if (match) {

    // Use the number in the match to create a new
    // key on the accumulator object if it doesn't exist,
    // and set it to an empty object
    acc[match[2]] = acc[match[2]] || {};

    // Now assign a value to the property in that object
    // identified by the match (title, to_date etc)
    acc[match[2]][match[1]] = value;
  }

  // Return the accumulated object for the next iteration
  return acc;
}, {});

console.log(Object.values(out));

【讨论】:

  • 谢谢。像魅力一样工作。您能否告诉我是否有任何简单的方法可以在不使用正则表达式的情况下做到这一点?
  • 我想不出一个。您需要能够识别密钥以及区分对象(数字)的方法,这似乎是最简单的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 2019-04-27
  • 2014-02-22
  • 2014-01-15
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
相关资源
最近更新 更多