将一个动态数组对象分组并形成所需的对象。

huangapple go评论54阅读模式
英文:

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 = [
  &quot;70669&quot;,
  &quot;70669|9436&quot;,
  &quot;70669|8353&quot;,
  &quot;70669|8914&quot;,
  &quot;70669|9522&quot;,
  &quot;70669|8422&quot;,
  &quot;70669|9639&quot;,
];

var result = [{ stdSizeCode: [], productHierSk: null }];

object.map((item) =&gt; {
  if (item.includes(&quot;|&quot;)) {
    result[0].stdSizeCode.push(parseInt(item.split(&quot;|&quot;)[1]));
  } else {
    result[0].productHierSk = parseInt(item);
  }
});

console.log(&quot;result : &quot;, result);

<!-- end snippet -->

答案3

得分: 0

使用 split解构赋值 一起,获取每个字符串的两个部分。

使用 mapNumber 将每个子字符串转换为数字。

参见 如何在 JavaScript 中将数组中的所有元素转换为整数?

reduce 调用将迭代您的数组并将所有内容合并到一个新的 Map 对象中。

Map 使查找和匹配您的 productHierSk 更容易。

如果您关心 object 中不同 productHierSk 出现的顺序,应该使用 Map 而不是普通对象。

使用 entriesArray.fromMap 被转换为一个包含 [ key , value ] 对的数组(在这种情况下,keyproductHierSkvalue 是数组 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 productHierSks.
If you care about the order in which different productHierSks 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 = [
    &quot;70669&quot;,
    &quot;70669|9436&quot;,
    &quot;70669|8353&quot;,
    &quot;70669|8914&quot;,
    &quot;70669|9522&quot;,
    &quot;70669|8422&quot;,
    &quot;70669|9639&quot;
  ],
  object2 = [
    &quot;1&quot;,
    &quot;2|5&quot;,
    &quot;1|2&quot;,
    &quot;2&quot;
  ];

const getSizeCodes = (object) =&gt; Array.from(object.reduce((stdSizeCodes, string) =&gt; {
    const [
        productHierSk,
        stdSizeCode
      ] = string.split(&quot;|&quot;).map(Number);

    if(!stdSizeCodes.has(productHierSk)){
      stdSizeCodes.set(productHierSk, []);
    }

    if(stdSizeCode){
      stdSizeCodes.get(productHierSk).push(stdSizeCode);
    }

    return stdSizeCodes;
  }, new Map()).entries())
    .map(([ productHierSk, stdSizeCode ]) =&gt; ({
      productHierSk,
      stdSizeCode
    }));

console.log(getSizeCodes(object1));
console.log(getSizeCodes(object2));

<!-- language: lang-css -->

.as-console-wrapper { max-height: 100% !important; top: 0; }

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月24日 01:30:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定