基于对象属性值的 JS 对象转换
JS Object transformation based on values of object properties
我看过很多其他与js对象排序相关的问题,其中大部分倾向于建议使用.map方法根据a的值对对象或对象数组进行排序属性,但我正在尝试实现一些稍微不同的东西。
我正在尝试转换此对象格式:
{
"commits": [
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
},
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
},
{
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}
]
}
进入一个使用 'repository' 值格式化的对象,如下所示:
{
"example-repo-1": [
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
},
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
}
],
"example-repo-2": [
{
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}
]
}
所以我需要将我的原始对象(一个包含其他对象数组的对象)转换为 return 一个包含大量数组的对象,以存储库的值命名 属性并包含与 属性 值匹配的每个对象。
使用Array#forEach
方法
var data = {
"commits": [{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
}, {
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
}, {
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}]
};
var res = {};
data.commits.forEach(function(v) {
// define the pproperty if already not defined
res[v.repository] = res[v.repository] || [];
// push the reference to the object or recreate depense on your need
res[v.repository].push(v);
})
console.log(res);
或使用Array#reduce
方法
var data = {
"commits": [{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
}, {
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
}, {
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}]
};
var res = data.commits.reduce(function(obj, v) {
// define property if not defined
obj[v.repository] = obj[v.repository] || [];
// push the object
obj[v.repository].push(v);
// return the result object
return obj;
}, {})
console.log(res);
尝试这样的事情
var input = {
"commits": [
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
},
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
},
{
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}
]
};
var output = {};
input.commits.forEach(function(el){
if(!output[el.repository])
output[el.repository] = [];
output[el.repository].push[el];
})
console.log(output);
我看过很多其他与js对象排序相关的问题,其中大部分倾向于建议使用.map方法根据a的值对对象或对象数组进行排序属性,但我正在尝试实现一些稍微不同的东西。
我正在尝试转换此对象格式:
{
"commits": [
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
},
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
},
{
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}
]
}
进入一个使用 'repository' 值格式化的对象,如下所示:
{
"example-repo-1": [
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
},
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
}
],
"example-repo-2": [
{
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}
]
}
所以我需要将我的原始对象(一个包含其他对象数组的对象)转换为 return 一个包含大量数组的对象,以存储库的值命名 属性并包含与 属性 值匹配的每个对象。
使用Array#forEach
方法
var data = {
"commits": [{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
}, {
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
}, {
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}]
};
var res = {};
data.commits.forEach(function(v) {
// define the pproperty if already not defined
res[v.repository] = res[v.repository] || [];
// push the reference to the object or recreate depense on your need
res[v.repository].push(v);
})
console.log(res);
或使用Array#reduce
方法
var data = {
"commits": [{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
}, {
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
}, {
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}]
};
var res = data.commits.reduce(function(obj, v) {
// define property if not defined
obj[v.repository] = obj[v.repository] || [];
// push the object
obj[v.repository].push(v);
// return the result object
return obj;
}, {})
console.log(res);
尝试这样的事情
var input = {
"commits": [
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1"
},
{
"repository": "example-repo-1",
"commit_hash": "example-hash-1.2"
},
{
"repository": "example-repo-2",
"commit_hash": "example-hash-2"
}
]
};
var output = {};
input.commits.forEach(function(el){
if(!output[el.repository])
output[el.repository] = [];
output[el.repository].push[el];
})
console.log(output);