将数据解析为JSON

时间:2018-11-14 20:47:07

标签: javascript arrays json parsing

我有一个特定的问题,我很难找到答案。

这是代码:

records.forEach(function(record){
    var personID = record.id;
    var personsInterest = record.interest;

    console.log(personID);
    console.log(personsInterest);
    console.log();
}

它输出以下内容:

138
death note

146
poop

138
poop

146
rick and morty

138
rick and morty

138
lil peep

145
420

我真的很喜欢这样的代码来存储数据

[
    {
        id: "138",
        interests:["death note","poop","rick and morty","lil peep"]
    },
    {
        id: "146",
        interests:["poop","rick and morty"]
    },
    {
        id: "145",
        interests:["420"]
    }
] 

根据兴趣数组的长度对其进行排序

3 个答案:

答案 0 :(得分:0)

这应该有效。在注释中注释。

const records = [{
    id: 138,
    interest: 'death note'
  },

  {
    id: 146,
    interest: 'poop'
  },

  {
    id: 138,
    interest: 'poop'
  },

  {
    id: 146,
    interest: 'rick and morty'
  },

  {
    id: 138,
    interest: 'rick and morty'
  },

  {
    id: 138,
    interest: 'lil peep'
  }
];

const sorted = records.reduce((all, record) => {
  const person = all.find(p => p.id === record.id);
  //If already exists person now contains existing person
  if (person) {
    person.interests.push(record.interest);
    //add new interest
  } else all.push({
    id: record.id,
    interests: [record.interest]
    //creates new person object
  });

  return all;
}, []).sort((a, b) => b.interests.length - a.interests.length);
//sorts based on length of interest array

console.log(sorted);

答案 1 :(得分:0)

要获得预期结果,请使用以下选项

  1. 使用forEach循环主要arr
  2. 创建结果arr
  3. 将新对象添加到结果rr
  4. 其他推送到兴趣数组

let records = [
  {
    id: 138,
    interests: "death note"
  },
  {
    id: 146,
    interests: "poop"
  },
  {
    id: 138,
    interests: "poop"
  },
  {
    id: 146,
    interests: "rick and morty"
  },
  {
    id: 138,
    interests: "rick and morty"
  },
  {
    id: 138,
    interests: "lil peep"
  },
  {
    id: 145,
    interests: "420"
  }
];

let result = [];
records.forEach(record => {
  if (result.filter(item => item.id == record.id).length == 0) {
    // check whether id exists in result array, if not push object to result array
    result.push({ id: record.id, interests: [record.interests] });
  } else {
    //if id already exists in result array, push interest to interest array of that id
    result.forEach(v => {
      if (v.id == record.id) {
        v.interests.push(record.interests);
      }
    });
  }
});

console.log(result);

codepen-https://codepen.io/nagasai/pen/Pxmwjp?editors=1010

答案 2 :(得分:0)

使用具有合适的compareFunction的sort方法对数组进行排序。 arr.sort(compareFunction(a,b))

Sort方法接受一个函数,并根据提供的函数的返回值对数组的元素进行排序。我们将其称为函数compareFunction(a, b)
如果compareFunction(a, b)的返回值小于0:a优先。
如果compareFunction(a, b)的返回值大于0:b优先。
如果compareFunction(a, b)的返回值为0:则不执行任何操作。

您想要具有较高兴趣的元素。长度排在第一位。这样我们就可以返回b.interest.length - a.interest.length

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

var records = [{
    id: 10,
    interest: ["poop", "rick and morty"]
  },
  {
    id: 11,
    interest: ["eminem", "snoop dog"]
  },
  {
    id: 12,
    interest: ["death note", "poop", "rick and morty", "lil peep"]
  },
  {
    id: 13,
    interest: ["god of war"]
  },
];

records = records.sort((a, b) => b.interest.length - a.interest.length);

console.log(records);

相关问题