使用 linq.js 从多个对象创建 JSON 个对象数组
Creat array of JSON objects from mulitple objects using linq.js
我有一个 JSON 数组,如下所示:
var jsonArray = [
{ Date: "2010-02-25", Size:"Large", Type:"a", Value: "100"},
{ Date: "2010-02-25", Size:"Medium", Type:"a", Value: "160"},
{ Date: "2010-02-25", Size:"Small", Type:"a", Value: "200"},
{ Date: "2010-02-25", Size:"Large", Type:"b", Value: "400"},
{ Date: "2010-02-25", Size:"Medium", Type:"b", Value: "120"},
{ Date: "2010-02-25", Size:"Small", Type:"b", Value: "170"}
]
我一直在使用 linq.js 并且我试图操纵上面的 JSON 所以 6 个对象变成了 2 个,所以我剩下一个如下所示的数组:
var desiredArray = [
{ Date: "2010-02-25", Large:"100", Medium: "160", Small: "200", Type:"a" }
{ Date: "2010-02-25", Large:"400", Medium: "120", Small: "170", Type:"b" }
]
不过我还是第一次用linq。我花了很长时间想弄明白,但我真的不知道如何开始。
谁能指出我正确的方向?
任何帮助将不胜感激
这是实现此目的的一种方法:
var jsonArray = [
{ Date: "2010-02-25", Size:"Large", Type:"a", Value: "100"},
{ Date: "2010-02-25", Size:"Medium", Type:"a", Value: "160"},
{ Date: "2010-02-25", Size:"Small", Type:"a", Value: "200"},
{ Date: "2010-02-25", Size:"Large", Type:"b", Value: "400"},
{ Date: "2010-02-25", Size:"Medium", Type:"b", Value: "120"},
{ Date: "2010-02-25", Size:"Small", Type:"b", Value: "170"}
];
function makeGroupKey(item) {
return JSON.stringify({
Date: item.Date,
Type: item.Type
});
}
function mergeItems(items) {
return items.Aggregate(JSON.parse(items.Key()), function (prev, next) {
prev[next.Size] = next.Value;
return prev;
});
}
var result = Enumerable.From(jsonArray)
.GroupBy(makeGroupKey)
.Select(mergeItems)
.ToArray();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>
我有一个 JSON 数组,如下所示:
var jsonArray = [
{ Date: "2010-02-25", Size:"Large", Type:"a", Value: "100"},
{ Date: "2010-02-25", Size:"Medium", Type:"a", Value: "160"},
{ Date: "2010-02-25", Size:"Small", Type:"a", Value: "200"},
{ Date: "2010-02-25", Size:"Large", Type:"b", Value: "400"},
{ Date: "2010-02-25", Size:"Medium", Type:"b", Value: "120"},
{ Date: "2010-02-25", Size:"Small", Type:"b", Value: "170"}
]
我一直在使用 linq.js 并且我试图操纵上面的 JSON 所以 6 个对象变成了 2 个,所以我剩下一个如下所示的数组:
var desiredArray = [
{ Date: "2010-02-25", Large:"100", Medium: "160", Small: "200", Type:"a" }
{ Date: "2010-02-25", Large:"400", Medium: "120", Small: "170", Type:"b" }
]
不过我还是第一次用linq。我花了很长时间想弄明白,但我真的不知道如何开始。
谁能指出我正确的方向?
任何帮助将不胜感激
这是实现此目的的一种方法:
var jsonArray = [
{ Date: "2010-02-25", Size:"Large", Type:"a", Value: "100"},
{ Date: "2010-02-25", Size:"Medium", Type:"a", Value: "160"},
{ Date: "2010-02-25", Size:"Small", Type:"a", Value: "200"},
{ Date: "2010-02-25", Size:"Large", Type:"b", Value: "400"},
{ Date: "2010-02-25", Size:"Medium", Type:"b", Value: "120"},
{ Date: "2010-02-25", Size:"Small", Type:"b", Value: "170"}
];
function makeGroupKey(item) {
return JSON.stringify({
Date: item.Date,
Type: item.Type
});
}
function mergeItems(items) {
return items.Aggregate(JSON.parse(items.Key()), function (prev, next) {
prev[next.Size] = next.Value;
return prev;
});
}
var result = Enumerable.From(jsonArray)
.GroupBy(makeGroupKey)
.Select(mergeItems)
.ToArray();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/linq.js/2.2.0.2/linq.min.js"></script>