遍历数组并将重复的项目移动到单独的数组

时间:2017-05-23 14:47:44

标签: javascript jquery arrays json sorting

我想要一个特定键值对的数组。

var myObj = [
     {"name" : "Kanye", "song" : "Jesus Walks"},
     {"name" : "Axel Ross", "song" : "Thunder Struck"},
     {"name" : "Kate Perry", "song" : "Superbass"},
     {"name" : "Kanye", "song" : "Fade"},
     {"name" : "Kanye", "song" : "Old Kanye"},
];

我想得到的是两个看起来像这样的数组。

var cleanObj = [
     {"name" : "Kanye", "song" : "Jesus Walks"},
     {"name" : "Axel Ross", "song" : "Thunder Struck"},
     {"name" : "Kate Perry", "song" : "Superbass"},
];

var duplicateObj = [
     {"name" : "Kanye", "song" : "Jesus Walks"},
     {"name" : "Kanye", "song" : "Fade"},
     {"name" : "Kanye", "song" : "Old Kanye"},
];

目前我有一些看起来像这样的东西。

var duplicateObj = [];
var cleanObj = {};

$.map(myObj, function (obj) {
    var checkDuplicate = myObj.some(function (elem) {
        return elem.name == obj.name;
    });
    if (checkDuplicate) {
        var tempObj = {
            name: obj.name,
            song: obj.song
        }
        duplicateObj.push(tempObj);
    } else {
        var tempObj = {
            name: obj.name,
            song: obj.song
        }
        cleanObj.push(tempObj);
    }
});

这个问题是它不会移动重复对象的第一个实例。只有之后比较的那些。

我知道如何移动重复项的第一个实例吗?

提前谢谢。

5 个答案:

答案 0 :(得分:1)

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
FindVisualChildren

答案 1 :(得分:1)

您可以使用哈希表并过滤并对名称进行分组,然后渲染重复项。

var data = [{ name: "Kanye", song: "Jesus Walks" }, { name: "Axel Ross", song: "Thunder Struck" }, { name: "Kate Perry", song: "Superbass" }, { name: "Kanye", song: "Fade" }, { name: "Kanye", song: "Old Kanye" }],
    groups = Object.create(null),
    singles = data.filter(function (o) {
        if (!groups[o.name]) {
            groups[o.name] = [o];
            return true;
        }
        groups[o.name].push(o);
    }),
    duplicates = Object.keys(groups).reduce(function (r, k) {
        return r.concat(groups[k].length > 1 ? groups[k] : []);
    }, []);

console.log(singles);
console.log(duplicates);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

我会使用reduce迭代数组并创建一个包含重复数组和干净数组的对象。

使用Array.some我可以判断clean数组是否已经有这个艺术家并将他推送到重复的数组,否则 - 将它推送到干净的数组。

如果这是我第一次将此艺术家插入重复数组,我还会在干净数组中插入它的项目

var myObj = [
     {"name" : "Kanye", "song" : "Jesus Walks"},
     {"name" : "Axel Ross", "song" : "Thunder Struck"},
     {"name" : "Kate Perry", "song" : "Superbass"},
     {"name" : "Kanye", "song" : "Fade"},
     {"name" : "Kanye", "song" : "Old Kanye"},
];

var ans = myObj.reduce((prev,curr)=>{
  if (!prev.clean.some(item => item.name == curr.name)) {
    prev.clean.push(curr);
  }
  else {
    if (!prev.duplicate.some(item => item.name == curr.name)) {
      prev.duplicate.push(prev.clean.find(item => item.name == curr.name));
    }
    prev.duplicate.push(curr);
  }
  return prev;
},{duplicate:[], clean:[]})

let duplicate = ans.duplicate;
let clean = ans.clean;

console.log(duplicate);
console.log(clean);

答案 3 :(得分:0)

您可以使用Array原型的“过滤器”属性,希望这会有所帮助:

var myObj = [
     {"name" : "Kanye", "song" : "Jesus Walks"},
     {"name" : "Axel Ross", "song" : "Thunder Struck"},
     {"name" : "Kate Perry", "song" : "Superbass"},
     {"name" : "Kanye", "song" : "Fade"},
     {"name" : "Kanye", "song" : "Old Kanye"},
];
var singles = [];
var duplicates = [];

function exists(valor, onlySingles) {
  var cont = 0;
  for (item in myObj) {
    if (valor.name === myObj[item].name){
      cont++;
    }
  }
  if (cont > 1)
    return onlySingles ? false : true;
  else
    return onlySingles ? true : false;
}

singles = myObj.filter(exists, true);
duplicates = myObj.filter(exists, false);


console.log("singles:=>");
console.log(singles);
console.log("duplicates:=>");
console.log(duplicates);

答案 4 :(得分:0)

我会使用name出现的次数作为隔离的标准。

&#13;
&#13;
var myObj = [{
    "name": "Kanye",
    "song": "Jesus Walks"
  },
  {
    "name": "Axel Ross",
    "song": "Thunder Struck"
  },
  {
    "name": "Kate Perry",
    "song": "Superbass"
  },
  {
    "name": "Kanye",
    "song": "Fade"
  },
  {
    "name": "Kanye",
    "song": "Old Kanye"
  },
];

var cleanObj = [],
  duplicateObj = [];
var mNameCount = {};

myObj.forEach(function(obj) {
  if (!mNameCount[obj.name]) {
    mNameCount[obj.name] = 0;
    cleanObj.push(obj);
  }
  mNameCount[obj.name]++;
});

myObj.forEach(function(obj) {
  if (mNameCount[obj.name] > 1) {
    duplicateObj.push(obj);
  }
});
console.log(cleanObj);
console.log(duplicateObj);
&#13;
&#13;
&#13;