是否有内置的lodash函数来执行此操作:
let params = ['foo', 'bar', 'baz', 'zle'];
let newArray = [];
params.forEach((element, index) => {
let key = "name" + index;
newArray.push({ key: element })
});
console.log(newArray);
预期的输出应该是这样的:
var object = {
a: {
name1: "foo",
name2: "bar",
},
b: {
name1: "baz",
name2: "zle",
}
}
答案 0 :(得分:1)
这可以帮助您。我知道您应该提供更多真正想要实现的信息。但我有个想法可能会对您有所帮助。
let params = ['foo', 'bar', 'baz', 'zle'];
const keys = ['a', 'b', 'c', 'd']
let data = {}
while(params.length){
const [a, b] = params.splice(0, 2)
const key = keys.splice(0, 1)
data[key] = {
name1: a,
name2: b,
}
}
console.log(data)
输出将是:
{
a: {
name1: "foo",
name2: "bar",
},
b: {
name1: "baz",
name2: "zle",
}
}
答案 1 :(得分:1)
我已经解决了这个问题。解决此问题的方法是
Highcharts.chart(chart_area, {
chart: {
height: 550,
type: 'areaspline'
},
title: {
text: ''
},
credits: {
enabled: false
},
colors: ['#3397DA', '#5CB85C', '#F9C822', '#E64C3C', '#756BB1', '#76C3FF', '#8CD58C', '#FFDA6A', '#FF7683', '#BFB7F4'],
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
day: '%d. %b',
month: '%b'%y',
year: '%Y'
}
},
plotOptions: {
areaspline: {
stacking: 'normal',
marker: {
lineWidth: 1,
enabled: false,
symbol: 'circle'
}
},
series: {
turboThreshold: 1000000,
cursor: 'pointer'
},
},
yAxis: [{
title: {
text: 'xyz'
},
}],
series: series
});
答案 2 :(得分:1)
您可以像这样将数组转换为Json格式,
var jsonObj = {};
for (var i = 0 ; i < sampleArray.length; i++) {
jsonObj["position" + (i+1)] = sampleArray[i];
}