简体   繁体   English

如何可扩展地解决这个数组操作问题?

[英]How can this array manipulation problem be solved scalably?

I am working with JS and came across an interesting Array manipulation problem.我正在使用 JS 并遇到了一个有趣的数组操作问题。 I'm aware that the problem can be solved with a tremendous if - else if clause, but am actually looking for a more scalable answer.我知道这个问题可以通过一个巨大的if - else if子句来解决,但实际上我正在寻找一个更具可扩展性的答案。

I have two arrays (let's call them input and output ) whose contents are related to each other.我有两个 arrays (我们称它们为inputoutput ),它们的内容彼此相关。 The first array contains a list of ids, and the second array is actually a response from an API (I have no power over the API) matching the ids of the input array.第一个数组包含一个 id 列表,第二个数组实际上是来自与输入数组的 id 匹配的 API(我对 API 没有权力)的响应。


Examples: If I have an input array of [1, 2, 3] I will end up with an output array of示例:如果我有一个[1, 2, 3]的输入数组,我将得到一个 output 数组

[
  { id: 1, data: "Some data for id 1" },
  { id: 2, data: "Some data for id 2" },
  { id: 3, data: "Some data for id 3" }
]

So far so good.到目前为止,一切都很好。 However, the problem arises if I have an input array like [1, 2, 2] , which is possible and intentional.但是,如果我有一个类似[1, 2, 2]的输入数组,就会出现问题,这是可能的并且是有意的。 In this situation I end with an output array of在这种情况下,我以 output 数组结束

[
  { id: 1, data: "Some data for id 1" },
  { id: 2, data: "Some data for id 2" }
]

In the above case I would need to have the output array be of length === 3 and the third item to be a copy of the second item like so在上述情况下,我需要让 output 数组的length === 3并且第三项是第二项的副本,如下所示

[
  { id: 1, data: "Some data for id 1" },
  { id: 2, data: "Some data for id 2" },
  { id: 2, data: "Some data for id 2" }
]

Two more examples for clarity: Input: [4, 4, 4] , what I get:为了清楚起见,再举两个例子: Input: [4, 4, 4] ,我得到的是:

[
  { id: 4, data: "Some data for id 4" }
]

What I need to achieve:我需要达到的目标:

[
  { id: 4, data: "Some data for id 4" },
  { id: 4, data: "Some data for id 4" },
  { id: 4, data: "Some data for id 4" }
]

Last one, input: [3, 3, 7] What I get:最后一个,输入: [3, 3, 7]我得到了什么:

[
  { id: 3, data: "Some data for id 3" },
  { id: 7, data: "Some data for id 7" }
]

What I need to achieve:我需要达到的目标:

[
  { id: 3, data: "Some data for id 3" },
  { id: 3, data: "Some data for id 3" },
  { id: 7, data: "Some data for id 7" },
]

I am working with a maximum array length of three, so an if-else clause comparing lengths and values is sufficient for this, but is there a dynamic and scalable way to manipulate the output array in the intended way?我使用的最大数组长度为 3,因此比较长度和值的if-else子句就足够了,但是是否有一种动态且可扩展的方式来按预期方式操作 output 数组? I was not able to think if anything from my mind.我无法思考是否有任何想法。

EDIT: I should add that the data part can be anything, not just the text I am giving here.编辑:我应该补充一点,数据部分可以是任何东西,而不仅仅是我在这里给出的文本。

Thanks in advance!提前致谢!

The map function allows us to create new arrays from a pre-existing one. map function 允许我们从现有的 arrays 创建新的 arrays。

const outputArray = inputArray.map(id => {
    return {
        id: id,
        data: "Some data for id " + id
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM