Javascript 如果数组中的项彼此相邻,则删除它们并获得重复计数
我试图制作一个类似于浏览器中的控制台,但在显示重复消息时遇到了困难,即如果您键入Javascript 如果数组中的项彼此相邻,则删除它们并获得重复计数,javascript,arrays,Javascript,Arrays,我试图制作一个类似于浏览器中的控制台,但在显示重复消息时遇到了困难,即如果您键入[…Array(10)].map(x=>console.log(“hello”),它将在控制台中打印(10)hello 假设我得到了这个数组: const array = [ { output: "testing", }, { output: "testing", }, { output:
[…Array(10)].map(x=>console.log(“hello”)
,它将在控制台中打印(10)hello
假设我得到了这个数组:
const array = [
{
output: "testing",
},
{
output: "testing",
},
{
output: "hello",
},
{
output: "world",
},
{
output: "world",
},
{
output: "testing",
},
]
所需的输出如下所示:
(2)testing
hello
(2)world
testing
如果重复项彼此相邻,我如何删除它们,并添加一个计数器,显示一行中有多少重复的消息。输出应该是这样的
const newArray = [
{
output: "testing",
count: 2
},
{
output: "hello",
count: 1
},
{
output: "world",
count: 2
},
{
output: "testing",
count: 1
},
]
我想可能是这样的
const数组=[{
输出:“测试”,
},
{
输出:“测试”,
},
{
输出:“你好”,
},
{
输出:“世界”,
},
{
输出:“世界”,
},
{
输出:“测试”,
},
]
设newArray=[];
for(设i=0;i1&&array[i]。输出==newArray[newArray.length-1]。输出){
newArray[newArray.length-1]。计数器+=1
}否则{
newArray.push({
输出:数组[i]。输出,
柜台:0
})
}
}
console.log(newArray)
过滤数组,然后打印
下面的例子
var数组=[
{
输出:“测试”,
},
{
输出:“测试”,
},
{
输出:“你好”,
},
{
输出:“世界”,
},
{
输出:“世界”,
},
{
输出:“测试”,
},
]
//您可以将阵列缩小为单个对象
数据=数组.reduce((acc,val)=>{
如果(val.output==acc.last_值){
acc.result[acc.result.length-1]。计数+=1;
}否则{
acc.result.push({…val,计数:1})
}
acc.last_值=val.output;
返回acc;
},{result:[],最后一个_值:null});
console.log(data.result)代码>更新:现在它将彼此靠近的条目分组
const数组=[
{输出:“测试”},
{输出:“测试”},
{输出:“你好”},
{输出:“世界”},
{输出:“世界”},
{输出:“测试”}
];
//结果对象
设subRes=false;
常数res=[];
//环路
for(设i=0;i您可以使用来管理它并更轻松地重用它
版本1
const array = [...]
const reduceCount = (acc, obj) => {
const key = obj["output"];
// check if we already store the key
if(!(key in acc)){
//the key is not register yet, so we create a counter state
acc[key] = {count: 0};
}
//increment the key state counter
acc[key]['count'] += 1
return acc;
}
// Reduce the array to get state of each key
const dictCount = array.reduce(reduceCount, {});
console.log(dictCount) // { testing: { count: 3 }, hello: { count: 1 }, world: { count: 2 } }
const newArray = Object.keys(dictCount).map(key => ({
output: key,
count: dictCount[key]['count']
}));
console.log(newArray)
/** output
[
{ output: 'testing', count: 3 },
{ output: 'hello', count: 1 },
{ output: 'world', count: 2 }
]
*/
版本2:更可重用的方式:
const array = [...]
const reduceCount = target => (acc, obj) => {
const key = obj[target];
// check if we already store the key
if(!(key in acc)){
//the key is not register yet, so we create a counter state
acc[key] = {count: 0};
}
//increment the key state counter
acc[key]['count'] += 1
return acc;
}
const counterState = target => arr => arr.reduce(reduceCount(target), {});
const counterStateOutput = counterState('output'); //specialize the function
// transform state to array
const stateToArray = state => Object.keys(state).map(key => ({
key,
count: state[key]["count"]
}));
console.log(stateToArray(counterStateOutput(array)))
//or
console.log(stateToArray(counterState('output')(array)))
/** output
[
{ key: 'testing', count: 3 },
{ key: 'hello', count: 1 },
{ key: 'world', count: 2 }
]
*/
你尝试了什么?@Mitya my bad updated,无法解决这个简单的问题有点沮丧Hehet这不考虑项目是否彼此相邻,或者现在切掉它,这就是你试图做的吗?我不想得到唯一的项目,我需要得到相同项目的计数,如果它们在阵列中与类似的项目相邻