仅将重复项从阵列推送到新阵列(以便可以将它们添加在一起)

时间:2018-09-09 07:35:35

标签: javascript php arrays push

我已经看到了许多关于如何删除重复项的答案,但我需要相反的答案。 我有重复的数组。我需要形成仅包含重复项的新数组。但是重复项未知,需要首先发现,因此整个过程是自动化的。

if ($_GET['weapontypeselect'])
	{
			$sql="SELECT * FROM `weapons` WHERE `weapon_type` = ".$_GET['weapontypeselect']."   ";
	}

然后这变得更加复杂,因为每个重复项都需要被推入其自己的数组中,并且还有一个更好的方法是,如果数组中只有2个以上的项,则可以这样做。

(最终目的是计算重复项的总数,因此也许有更好的解决方案?)

最终结果应该类似于:

    array = [{
    "name": "cat",
    "amount": 1
    }, {
    "name": "cat",
    "amount": 3
    }, {
    "name": "cat",
    "amount": 2
    }, {
    "name": "dog",
    "amount": 5
    }, {
    "name": "dog",
    "amount": 3
    }, {
    "name": "dog",
    "amount": 2
    }, {
    "name": "dog",
    "amount": 5
    }, {
    "name": "duck",
    "amount": 1
    }, {
    "name": "duck",
    "amount": 4
    }]

但是实际的最终结果只是简单地将重复项总计在一起(但是,如果可以生成先前的数组,我已经可以得到):

array1 = [{
    "name": "cat",
    "amount": 1
    }, {
    "name": "cat",
    "amount": 3
    }, {
    "name": "cat",
    "amount": 2
    }]

array2 = [{
    "name": "dog",
    "amount": 5
    }, {
    "name": "dog",
    "amount": 3
    }, {
    "name": "dog",
    "amount": 2
    }, {
    "name": "dog",
    "amount": 5
    }]

尝试通过Javascript或PHP获得此结果。

到目前为止,我最接近的是使用PHP将唯一项放入新数组中:

array = [{
    "name": "cat",
    "amount": 6
    }, {
    "name": "dog",
    "amount": 15
    }]

但这只是给出重复项。

4 个答案:

答案 0 :(得分:1)

尝试一下:

var array = [{
    "name": "cat",
    "amount": 1
}, {
    "name": "cat",
    "amount": 3
}, {
    "name": "cat",
    "amount": 2
}, {
    "name": "dog",
    "amount": 5
}, {
    "name": "dog",
    "amount": 3
}, {
    "name": "dog",
    "amount": 2
}, {
    "name": "dog",
    "amount": 5
}, {
    "name": "duck",
    "amount": 1
}, {
    "name": "duck",
    "amount": 4
}];

var data = {};
for (var elem of array) {
    data[elem.name] = data[elem.name] || []
    if (data[elem.name]) {
        data[elem.name].push(elem);
    } else {
        data[elem.name] = [elem];
    }
}

var result = [];

for (var key in data) {
    var amounts = 0;
    for (var elem of data[key]) {
        amounts += elem.amount;
    }
    result.push({
        name: key,
        amount: amounts
    });
}


console.log(result);
// [{
//     "name": "cat",
//     "amount": 6
// }, {
//     "name": "dog",
//     "amount": 15
// }, {
//     "name": "duck",
//     "amount": 5
// }]

答案 1 :(得分:0)

您可以使用Map并为已分组的对象获取一个数组数组。

var data = [{ name: "cat", amount: 1 }, { name: "cat", amount: 3 }, { name: "cat", amount: 2 }, { name: "dog", amount: 5 }, { name: "dog", amount: 3 }, { name: "dog", amount: 2 }, { name: "dog", amount: 5 }, { name: "duck", amount: 1 }, { name: "duck", amount: 4 }],
    result = Array.from(
        data.reduce((m, o) => m.set(o.name, (m.get(o.name) || []).concat(o)), new Map).values()
    );

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

答案 2 :(得分:0)

直接获得最终结果怎么样?如果不存在则推送到新数组,如果已经存在则累计数量

array = [{
    "name": "cat",
    "amount": 1
    }, {
    "name": "cat",
    "amount": 3
    }, {
    "name": "cat",
    "amount": 2
    }, {
    "name": "dog",
    "amount": 5
    }, {
    "name": "dog",
    "amount": 3
    }, {
    "name": "dog",
    "amount": 2
    }, {
    "name": "dog",
    "amount": 5
    }, {
    "name": "duck",
    "amount": 1
    }, {
    "name": "duck",
    "amount": 4
}];
let res = array.reduce((res, elem) => {
  let i = res.filter((e, i) => {
    if (e.name === elem.name) {
      e.amount += elem.amount
      return true
    }
    return false
  })
  if (i.length === 0) res.push(elem)
  return res
}, [])
console.log(res)

答案 3 :(得分:0)

这个怎么样? 绝对可以重构,但是您会明白的:)

const dupa = array.reduce((acc, { name, amount }) => {
  if (acc[name]) {
    return { ...acc, [name]: { amount: acc[name].amount + amount, count: acc[name].count += 1 }};
  }
  return { ...acc, [name]: { amount, count: 1}};
}, {});

const jaje = Object.keys(dupa)
  .filter(key => dupa[key].count > 2)
  .map(key => {
    return { name: key, amount: dupa[key].amount }    
})

console.log(jaje)

工作笔在这里。 https://codepen.io/_papiscript/pen/VGymjL?editors=0010