【问题标题】:How to get json data that has same key javascript [duplicate]如何获取具有相同键javascript的json数据[重复]
【发布时间】:2020-09-21 09:28:24
【问题描述】:

我有类及其句子的数据(训练数据),我该如何转换这个 json 数据

[
  { class: 'Reservation', sentence: 'make reservation' },
  { class: 'Reservation', sentence: 'do reservation' },
  { class: 'Greetings', sentence: 'Hello' },
  { class: 'Greetings', sentence: 'Good Morning' }
]

转成这种格式的数据:

[
  { class: 'Reservation', sentence: ['make reservation','do reservation'] },
  { class: 'Greetings', sentence: ['Hello','Good Morning'] }
]

【问题讨论】:

  • 你已经尝试过什么了吗?
  • 向我们展示您失败的代码。提示:您只需将它们分组到class

标签: javascript json


【解决方案1】:

您可以遍历每个对象并将它们添加到新数组中。添加之前检查对象是否已经在新数组中,如果是,则可以将当前句子推送到现有对象,而不是添加。

步骤

  1. 创建一个空数组
  2. 循环遍历对象数组(不是新数组,您的原始对象数组)
  3. 检查具有类值的对象是否已经存在
  4. 如果否,则将新对象推送到新数组,其中class 的值是您的类,sentence 的值是一个数组,其中sentence 是第一项(请参阅下面的代码)。如果是,则将当前句子推送到现有对象的句子数组中。

var data = [
  { class: 'Reservation', sentence: 'make reservation' },
  { class: 'Reservation', sentence: 'do reservation' },
  { class: 'Greetings', sentence: 'Hello' },
  { class: 'Greetings', sentence: 'Good Morning' }
]

var dataFormatted = [];

for(var i = 0; i < data.length; i++){
    //document.write(JSON.stringify(data[i]));
  const existingClass = dataFormatted.find(d => d.class == data[i].class);
  if(existingClass){
    existingClass.sentence.push(data[i].sentence)
  } else{
    dataFormatted.push({
        class: data[i].class,
      sentence: [data[i].sentence]
    });
  }
}

document.write(JSON.stringify(dataFormatted))

希望它有意义。

【讨论】:

    【解决方案2】:

    您可以查看 json 数组并通过尝试追加到句子列表或在未找到现有结果时创建新项目来分配新数组:

    const json = [
      { class: 'Reservation', sentence: 'make reservation' },
      { class: 'Reservation', sentence: 'do reservation' },
      { class: 'Greetings', sentence: 'Hello' },
      { class: 'Greetings', sentence: 'Good Morning' }
    ]
    
    const data = []
    json.forEach((item) => {
      let found = data.find(e => e.class === item.class);
      if (found) {
        found.sentence.push(item.sentence);
      } else {
        data.push({
          class: item.class,
          sentence: [item.sentence]
        });
      }
    })
    
    
    console.log(data)

    【讨论】:

      【解决方案3】:

      也许你想consolidate 喜欢:

      function consolidate(array){
        const r = [];
        for(let i=0,o,p,l=array.length; i<l; i++){
          o = array[i]; p = r.find(d=>d.class === o.class);
          if(p){
            p.sentence.push(o.sentence);
          }
          else{
            r.push({class:o.class, sentence:[o.sentence]}); 
          }
        }
        return r;
      }
      const testArray = [
        { class: 'Reservation', sentence: 'make reservation' },
        { class: 'Reservation', sentence: 'do reservation' },
        { class: 'Greetings', sentence: 'Hello' },
        { class: 'Greetings', sentence: 'Good Morning' }
      ];
      console.log(consolidate(testArray));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-07
        • 2016-12-15
        • 2013-05-12
        • 2021-03-18
        • 2017-07-20
        • 1970-01-01
        • 1970-01-01
        • 2015-08-30
        相关资源
        最近更新 更多