英文:
Grouping a dynamic array object and forming a object required
问题
我有一个类似这样的动态对象
var object = [
"70669",
"70669|9436",
"70669|8353",
"70669|8914",
"70669|9522",
"70669|8422",
"70669|9639"
]
我的期望输出是这样的
[{
stdSizeCode: [9436, 8353, 8914, 9522, 8422, 9639],
productHierSk: 70669
}]
我已经尝试查看这个链接,但它的工作方式不如预期。有人可以提出建议吗?
英文:
I have a dynamic object something like this
var object = [
"70669",
"70669|9436",
"70669|8353",
"70669|8914",
"70669|9522",
"70669|8422",
"70669|9639"
]
My expected output is something like this
[{
stdSizeCode: [9436, 8353, 8914, 9522, 8422, 9639]
productHierSk: 70669
}]
I have tried to looking into this link and did not work the way that is expected. Can someone please suggest
答案1
得分: 1
你可以使用一个对象进行分组,并将值作为结果获取。
const data = ["70669", "70669|9436", "70669|8353", "70669|8914", "70669|9522", "70669|8422", "70669|9639"];
const result = Object.values(data.reduce((r, s) => {
const [productHierSk, code] = s.split('|').map(Number);
r[productHierSk] ??= { stdSizeCode: [], productHierSk };
if (code !== undefined) r[productHierSk].stdSizeCode.push(code);
return r;
}, []));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:
You could group by with an object and get the values as result.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
data = ["70669", "70669|9436", "70669|8353", "70669|8914", "70669|9522", "70669|8422", "70669|9639"],
result = Object.values(data.reduce((r, s) => {
const [productHierSk, code] = s.split('|').map(Number);
r[productHierSk] ??= { stdSizeCode: [], productHierSk };
if (code !== undefined) r[productHierSk].stdSizeCode.push(code);
return r;
}, []));
console.log(result);
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
答案2
得分: 0
这部分的内容已经被翻译为中文如下:
如果我正确理解您的问题
它工作正常:
var object = [
"70669",
"70669|9436",
"70669|8353",
"70669|8914",
"70669|9522",
"70669|8422",
"70669|9639",
];
var result = [{ stdSizeCode: [], productHierSk: null }];
object.map((item) => {
if (item.includes("|")) {
result[0].stdSizeCode.push(parseInt(item.split("|")[1]));
} else {
result[0].productHierSk = parseInt(item);
}
});
console.log("result : ", result);
希望这对您有帮助。如果您需要任何进一步的帮助,请告诉我。
英文:
If I understand your question correctly
It work fine :
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var object = [
"70669",
"70669|9436",
"70669|8353",
"70669|8914",
"70669|9522",
"70669|8422",
"70669|9639",
];
var result = [{ stdSizeCode: [], productHierSk: null }];
object.map((item) => {
if (item.includes("|")) {
result[0].stdSizeCode.push(parseInt(item.split("|")[1]));
} else {
result[0].productHierSk = parseInt(item);
}
});
console.log("result : ", result);
<!-- end snippet -->
答案3
得分: 0
使用 split
与 解构赋值 一起,获取每个字符串的两个部分。
参见 如何在 JavaScript 中将数组中的所有元素转换为整数?。
reduce
调用将迭代您的数组并将所有内容合并到一个新的 Map
对象中。
Map
使查找和匹配您的 productHierSk
更容易。
如果您关心 object
中不同 productHierSk
出现的顺序,应该使用 Map
而不是普通对象。
使用 entries
和 Array.from
,Map
被转换为一个包含 [
key ,
value ]
对的数组(在这种情况下,key 是 productHierSk
,value 是数组 stdSizeCode
)。
再使用一个 map
将每个这样的数组转换为最终对象。
英文:
Use split
together with the destructuring assignment to get both parts of each string.
Use map
with Number
to convert each substring to a number.
See How to convert all elements in an array to integer in JavaScript?.
The reduce
call will iterate over your array and merge everything into a new Map
object.
The Map
makes it easier to find and match your productHierSk
s.
If you care about the order in which different productHierSk
s occur in your object
, you should use a Map
instead of a plain object.
With entries
and Array.from
the Map
is transformed into an Array of [
key ,
value ]
pairs (in this case, the key is productHierSk
and the value is the Array stdSizeCode
).
One more map
transforms each of these into the final object.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const object1 = [
"70669",
"70669|9436",
"70669|8353",
"70669|8914",
"70669|9522",
"70669|8422",
"70669|9639"
],
object2 = [
"1",
"2|5",
"1|2",
"2"
];
const getSizeCodes = (object) => Array.from(object.reduce((stdSizeCodes, string) => {
const [
productHierSk,
stdSizeCode
] = string.split("|").map(Number);
if(!stdSizeCodes.has(productHierSk)){
stdSizeCodes.set(productHierSk, []);
}
if(stdSizeCode){
stdSizeCodes.get(productHierSk).push(stdSizeCode);
}
return stdSizeCodes;
}, new Map()).entries())
.map(([ productHierSk, stdSizeCode ]) => ({
productHierSk,
stdSizeCode
}));
console.log(getSizeCodes(object1));
console.log(getSizeCodes(object2));
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论