我在javascript中有数组:
var category : [{Kode: "AK, BK, CC, CS, DH, GR, KZ, MS, OD, YU "}]
和
var value : [{value: "376.98, 676.96, 477.23, 1248.89, 573.04, 432.45, 440.92, 397.41, 372.36, 376.59 "}]
我想输出:
var category : ['AK', 'BK', 'CC', 'CS', 'DH', 'GR', 'KZ', 'MS', 'OD', 'YU']
var value : [376.98, 676.96, 477.23, 1248.89, 573.04, 432.45, 440.92, 397.41, 372.36, 376.59]
请帮助我,谢谢:)
答案 0 :(得分:1)
只需尝试在javascript中拆分:
var cat_result= category[0].Kode.split(",");
var val_result= value[0].value.split(",");
效果很好!
答案 1 :(得分:0)
尝试使用此Object.keys() and Object.values()
方法。我提到两者都是个体变量
var category = [{
Kode: "AK, BK, CC, CS, DH, GR, KZ, MS, OD, YU "
}]
var value = [{
value: "376.98, 676.96, 477.23, 1248.89, 573.04, 432.45, 440.92, 397.41, 372.36, 376.59 "
}]
var res = {}
res[Object.keys(category[0])] = Object.values(category[0])[0].split(',');
res[Object.keys(value[0])] = Object.values(value[0])[0].split(',');
console.log(res)
使用单个对象
var arr = {
category: [{
Kode: "AK, BK, CC, CS, DH, GR, KZ, MS, OD, YU "
}],
value: [{
value: "376.98, 676.96, 477.23, 1248.89, 573.04, 432.45, 440.92, 397.41, 372.36, 376.59 "
}]
}
var res = {}
Object.values(arr).forEach(function(a){
res[Object.keys(a[0])] = Object.values(a[0])[0].split(',');
})
console.log(res)
答案 2 :(得分:0)
对于类别,您可以使用以下代码
var category = [{Kode: "AK, BK, CC, CS, DH, GR, KZ, MS, OD, YU "}];
var categoryList= category[0].Kode.split(",");
console.log(category[0].Kode.split(","));
对于值,您可以使用以下代码:
var value = [{value: "376.98, 676.96, 477.23, 1248.89, 573.04, 432.45, 440.92, 397.41, 372.36, 376.59 "}];
var valueList= value[0].value.split(",");
console.log(valueList)
答案 3 :(得分:-2)
你必须这样做。感谢
var category = [{
Kode: "AK, BK, CC, CS, DH, GR, KZ, MS, OD, YU "
}];
var array = $.map(category[0], function(value, index) {
return [value];
});
console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>