数组数据是混合的我希望重复日期并在数组中连接数字
var data = [
['mars 21 21:37', 624],
['mars 21 21:37', 599],
['mars 21 21:37', 636],
['mars 21 21:37', 619],
['mars 21 21:37', 625],
['mars 21 21:37', 645],
['mars 21 21:37', 638],
['mars 21 23:24', 733],
['mars 21 23:24', 800],
['mars 21 23:24', 609],
['mars 21 23:24', 615],
['mars 22 09:20', 550],
['mars 22 09:20', 608],
['mars 22 09:20', 609]
];
我正在寻找转化为
的解决方案var data = [{
'mars 21 21:37': [624, 599, 636, 619, 625, 645, 638],
'mars 21 23:24': [733, 800, 609, 615],
'mars 22 09:20': [550, 608, 609]
}];
任何想法?
答案 0 :(得分:1)
你可以这样做:
var data = [
[ 'mars 21 21:37', 624 ],
[ 'mars 21 21:37', 599 ],
[ 'mars 21 21:37', 636 ],
[ 'mars 21 21:37', 619 ],
[ 'mars 21 21:37', 625 ],
[ 'mars 21 21:37', 645 ],
[ 'mars 21 21:37', 638 ],
[ 'mars 21 23:24', 733 ],
[ 'mars 21 23:24', 800 ],
[ 'mars 21 23:24', 609 ],
[ 'mars 21 23:24', 615 ],
[ 'mars 22 09:20', 550 ],
[ 'mars 22 09:20', 608 ],
[ 'mars 22 09:20', 609 ]
];
let output = {};
data.forEach(([key, value]) => {
if (Array.isArray(output[key])) {
output[key].push(value);
} else {
output[key] = [value];
}
})
console.log(output);
如果你在减少之后,你可以做这样的事情:
var data = [
['mars 21 21:37', 624],
['mars 21 21:37', 599],
['mars 21 21:37', 636],
['mars 21 21:37', 619],
['mars 21 21:37', 625],
['mars 21 21:37', 645],
['mars 21 21:37', 638],
['mars 21 23:24', 733],
['mars 21 23:24', 800],
['mars 21 23:24', 609],
['mars 21 23:24', 615],
['mars 22 09:20', 550],
['mars 22 09:20', 608],
['mars 22 09:20', 609]
];
data = data.reduce((carry, [key, value]) => {
if (Array.isArray(carry[key])) {
carry[key].push(value)
} else {
carry[key] = [value]
}
return carry;
}, {});
console.log(data)
// If you're after the super compressed version you could do this:
// data = data.reduce((c,[k,v])=>(c[k]=[...(c[k]||[]),v],c),{});
答案 1 :(得分:1)
您可以将Array.prototype.reduce()与Array.prototype.concat()结合使用:
const data = [['mars 21 21:37', 624],['mars 21 21:37', 599],['mars 21 21:37', 636],['mars 21 21:37', 619],['mars 21 21:37', 625],['mars 21 21:37', 645],['mars 21 21:37', 638],['mars 21 23:24', 733],['mars 21 23:24', 800],['mars 21 23:24', 609],['mars 21 23:24', 615],['mars 22 09:20', 550],['mars 22 09:20', 608],['mars 22 09:20', 609]];
const result = data.reduce((a, [d, v]) => (a[d] = a[d] ? a[d].concat(v) : [v], a), {});
console.log(result);

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

答案 2 :(得分:0)
您可以使用reduce
方法并返回一个对象。
var data = [ [ 'mars 21 21:37', 624 ],[ 'mars 21 21:37', 599 ],[ 'mars 21 21:37', 636 ],[ 'mars 21 21:37', 619 ],[ 'mars 21 21:37', 625 ],[ 'mars 21 21:37', 645 ],[ 'mars 21 21:37', 638 ],[ 'mars 21 23:24', 733 ],[ 'mars 21 23:24', 800 ],[ 'mars 21 23:24', 609 ],[ 'mars 21 23:24', 615 ],[ 'mars 22 09:20', 550 ],[ 'mars 22 09:20', 608 ],[ 'mars 22 09:20', 609]];
const result = data.reduce((r, [time, value]) => {
if(!r[time]) r[time] = [value]
else r[time].push(value);
return r;
}, {})
console.log(result)

答案 3 :(得分:0)
您可以使用函数reduce
来构建所需的输出。
var data = [ [ 'mars 21 21:37', 624 ], [ 'mars 21 21:37', 599 ], [ 'mars 21 21:37', 636 ], [ 'mars 21 21:37', 619 ], [ 'mars 21 21:37', 625 ], [ 'mars 21 21:37', 645 ], [ 'mars 21 21:37', 638 ], [ 'mars 21 23:24', 733 ], [ 'mars 21 23:24', 800 ], [ 'mars 21 23:24', 609 ], [ 'mars 21 23:24', 615 ], [ 'mars 22 09:20', 550 ], [ 'mars 22 09:20', 608 ], [ 'mars 22 09:20', 609 ] ],
result = data.reduce((a, [date, value]) => ((a[date] || (a[date] = [])).push(value), a), {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 4 :(得分:0)
您可以先使用underscore.js
链式方法进行分组,然后再简化收集的值。
var data = [['mars 21 21:37', 624], ['mars 21 21:37', 599], ['mars 21 21:37', 636], ['mars 21 21:37', 619], ['mars 21 21:37', 625], ['mars 21 21:37', 645], ['mars 21 21:37', 638], ['mars 21 23:24', 733], ['mars 21 23:24', 800], ['mars 21 23:24', 609], ['mars 21 23:24', 615], ['mars 22 09:20', 550], ['mars 22 09:20', 608], ['mars 22 09:20', 609]];
console.log(_
.chain(data)
.groupBy(([date]) => date)
.mapObject(values => values.map(([, v]) => v))
.value()
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;