JavaScript 组对象
JavaScript group objects
我已经找了几天了,还没有找到这个具体问题的答案。我从我的 API 中的端点接收到一个 javascript 对象数组。我需要根据类型将对象组合在一起。
示例硬编码对象数组:
$scope.content = {
things: [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
}
var typeArr = [];
for (var key in $scope.content.things) {
typeArr[key] = $scope.content.things[key].thingType;
}
typeArr = _.uniq(typeArr);
typeArr 现在将是 [load, export] 接下来我需要做的是比较 things[ ] 中的所有对象,使得
if(typeArr[key] === things[i].thingType)
会像这样推动那个对象:
typeArr = [
load: {
thing: 'one',
thingType: 'load'
},
export: [{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
]
换句话说,我需要对象保持完整,我需要将它们分类,并根据共享类型嵌套它们。我整个星期都认真地坚持这个。任何见解将不胜感激。
这样的事情可以接受吗?
var things = [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
];
var types = {};
for (i in things) {
var thing = things[i];
if (typeof types[thing.thingType] === "undefined") {
types[thing.thingType] = [];
}
types[thing.thingType].push(thing);
}
console.log(types);
生成的 types
对象看起来像
{
load: [{
thing: 'one',
thingType: 'load'
}],
export: [{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}]
}
也就是说,每种类型始终是一个数组,即使它只包含一个项目(在您的示例中它只是一个对象)。但我认为无论如何你都会更容易处理,因为你知道你总是期望一个数组。
不是 100% 我知道你的目标是什么,但我会尝试一下。这是你的意思吗..
$scope.content = {
things: [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
}
$scope.groups = {};
$scope.group = group;
function group(){
angular.forEach($scope.content.things, function(thing){
if($scope.groups.hasOwnProperty(thing.thingType)){
$scope.groups[thing.thingType].push(thing);
} else {
$scope.groups[thing.thingType] = [thing];
}
});
}
试试这个
var content = {
things: [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
}
var typeArr = {};
content.things.forEach(function(item){
typeArr[item.thingType] = typeArr[item.thingType]||[];
typeArr[item.thingType].push(item);
});
document.body.innerHTML = JSON.stringify(typeArr);
我已经找了几天了,还没有找到这个具体问题的答案。我从我的 API 中的端点接收到一个 javascript 对象数组。我需要根据类型将对象组合在一起。
示例硬编码对象数组:
$scope.content = {
things: [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
}
var typeArr = [];
for (var key in $scope.content.things) {
typeArr[key] = $scope.content.things[key].thingType;
}
typeArr = _.uniq(typeArr);
typeArr 现在将是 [load, export] 接下来我需要做的是比较 things[ ] 中的所有对象,使得
if(typeArr[key] === things[i].thingType)
会像这样推动那个对象:
typeArr = [
load: {
thing: 'one',
thingType: 'load'
},
export: [{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
]
换句话说,我需要对象保持完整,我需要将它们分类,并根据共享类型嵌套它们。我整个星期都认真地坚持这个。任何见解将不胜感激。
这样的事情可以接受吗?
var things = [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
];
var types = {};
for (i in things) {
var thing = things[i];
if (typeof types[thing.thingType] === "undefined") {
types[thing.thingType] = [];
}
types[thing.thingType].push(thing);
}
console.log(types);
生成的 types
对象看起来像
{
load: [{
thing: 'one',
thingType: 'load'
}],
export: [{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}]
}
也就是说,每种类型始终是一个数组,即使它只包含一个项目(在您的示例中它只是一个对象)。但我认为无论如何你都会更容易处理,因为你知道你总是期望一个数组。
不是 100% 我知道你的目标是什么,但我会尝试一下。这是你的意思吗..
$scope.content = {
things: [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
}
$scope.groups = {};
$scope.group = group;
function group(){
angular.forEach($scope.content.things, function(thing){
if($scope.groups.hasOwnProperty(thing.thingType)){
$scope.groups[thing.thingType].push(thing);
} else {
$scope.groups[thing.thingType] = [thing];
}
});
}
试试这个
var content = {
things: [
{
thing: 'one',
thingType: 'load'
},
{
thing: 'two',
thingType: 'export'
},
{
thing: 'three',
thingType: 'export'
}
]
}
var typeArr = {};
content.things.forEach(function(item){
typeArr[item.thingType] = typeArr[item.thingType]||[];
typeArr[item.thingType].push(item);
});
document.body.innerHTML = JSON.stringify(typeArr);