将一个对象数组根据 id 数组拆分成较小的数组,使用 JavaScript。

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

Split an array of objects into smaller arrays based on the arrays of ids Javascript

问题

Here is the translated code snippet for your request:

我有一个初始对象数组和2个ID数组如下所示
```javascript
const arr = [
  { id: 1, name: "John" },
  { id: 7, name: "Alice" },
  { id: 10, name: "Ken" },
  { id: 5, name: "Bob" },
  { id: 2, name: "Kevin" },
  { id: 6, name: "Tony" },
  { id: 3, name: "Harry" },
  { id: 4, name: "Kyle" },
  { id: 8, name: "Jane" },
  { id: 9, name: "Sam" },
];
const list1 = [1, 4, 2];
const list2 = [3, 8, 9, 10];

我想要的是arr的两个新子数组,包括具有list1list2中相应ID的对象。请注意,两个新子数组中对象的顺序必须与list1list2中ID的顺序相同

我已经尝试了下面的方法,它可以工作,但在arr中有大量项的情况下可能不太有效:

const subArr1 = [];
const subArr2 = [];

list1.forEach((id) => {
  const idx = arr.findIndex((item) => item.id === id);
  if (idx !== -1 && subArr1.findIndex((item) => item.id === id) === -1) {
    subArr1.push(arr[idx]);
  }
});
list2.forEach((id) => {
  const idx = arr.findIndex((item) => item.id === id);
  if (idx !== -1 && subArr2.findIndex((item) => item.id === id) === -1) {
    subArr2.push(arr[idx]);
  }
});


/* 结果:
subArr1 = [
  { id: 1, name: "John" },
  { id: 4, name: "Kyle" },
  { id: 2, name: "Kevin" },
];
subArr2 = [
  { id: 3, name: "Harry" },
  { id: 8, name: "Jane" },
  { id: 9, name: "Sam" },
  { id: 10, name: "Ken" },
];
*/

除了我的想法,是否有更好的解决方案?

更新:

Array.prototype.filter()Array.prototype.includes()可用于创建2个新的子数组,但顺序也应与list1list2中的ID顺序相同。

英文:

I have an initial array of objects and 2 arrays of ids as following:

const arr = [
  { id: 1, name: "John" },
  { id: 7, name: "Alice" },
  { id: 10, name: "Ken" },
  { id: 5, name: "Bob" },
  { id: 2, name: "Kevin" },
  { id: 6, name: "Tony" },
  { id: 3, name: "Harry" },
  { id: 4, name: "Kyle" },
  { id: 8, name: "Jane" },
  { id: 9, name: "Sam" },
];
const list1 = [1, 4, 2];
const list2 = [3, 8, 9, 10];

What I want is 2 new sub arrays of arr which include the objects having the respective id in list1 and list2. Notice that the order of objects of two new sub arrays must be as the same order of ids in list1 and list2.

I have tried the below way, it works but may not be effective with a large number of items in arr:

const subArr1 = [];
const subArr2 = [];

list1.forEach((id) => {
  const idx = arr.findIndex((item) => item.id === id);
  if (idx !== -1 && subArr1.findIndex((item) => item.id === id) === -1) {
    subArr1.push(arr[idx]);
  }
});
list2.forEach((id) => {
  const idx = arr.findIndex((item) => item.id === id);
  if (idx !== -1 && subArr2.findIndex((item) => item.id === id) === -1) {
    subArr2.push(arr[idx]);
  }
});


/* Result:
subArr1 = [
  { id: 1, name: "John" },
  { id: 4, name: "Kyle" },
  { id: 2, name: "Kevin" },
];
subArr2 = [
  { id: 3, name: "Harry" },
  { id: 8, name: "Jane" },
  { id: 9, name: "Sam" },
  { id: 10, name: "Ken" },
];
*/

Beside my idea, is there any better solution?

Updated:

The Array.prototype.filter() and Array.prototype.includes() works to create 2 new sub arrays but the order also should be the same as order of ids in list1 and list2

答案1

得分: 0

const subArr1 = list1.map((id) => arr.find((item) => item.id === id));
const subArr2 = list2.map((id) => arr.find((item) => item.id === id));
英文:
const subArr1 = list1.map((id) => arr.find((item) => item.id === id));
const subArr2 = list2.map((id) => arr.find((item) => item.id === id));

答案2

得分: 0

这段代码包含一个名为arr的数组,其中包含具有id和name属性的对象。还有两个额外的数组list1和list2,它们包含一些ID。

代码的目标是基于list1和list2中的ID来过滤arr数组,从而得到两个子数组:subArr1和subArr2。

以下是解决方案的逐步说明:

  • 初始化一个空数组subArr1,用于存储根据list1中的ID过滤出的对象。
  • 在arr数组上使用filter方法来迭代每个对象。
  • 对于每个对象,使用includes方法检查其id是否存在于list1中。
  • 如果对象的id在list1中存在,它将返回true,并被包含在subArr1数组中。
  • 重复相同的步骤,通过根据list2中的ID过滤arr来创建subArr2。

执行完代码后,subArr1将包含具有ID 1、4和2的arr中的对象,而subArr2将包含具有ID 3、8、9和10的对象。

通过使用filter方法和includes方法,我们可以轻松地基于特定的ID来过滤对象数组,并将过滤后的对象存储在单独的子数组中。

英文:
const arr = [
  { id: 1, name: 'John' },
  { id: 7, name: 'Alice' },
  { id: 10, name: 'Ken' },
  { id: 5, name: 'Bob' },
  { id: 2, name: 'Kevin' },
  { id: 6, name: 'Tony' },
  { id: 3, name: 'Harry' },
  { id: 4, name: 'Kyle' },
  { id: 8, name: 'Jane' },
  { id: 9, name: 'Sam' },
];

const list1 = [1, 4, 2];
const list2 = [3, 8, 9, 10];

const subArr1 = arr.filter((item) => list1.includes(item.id));
const subArr2 = arr.filter((item) => list2.includes(item.id));

console.log({
  subArr1,
  subArr2,
});

/**
 * Output:
 * {
  subArr1: [
    { id: 1, name: 'John' },
    { id: 2, name: 'Kevin' },
    { id: 4, name: 'Kyle' }
  ],
  subArr2: [
    { id: 10, name: 'Ken' },
    { id: 3, name: 'Harry' },
    { id: 8, name: 'Jane' },
    { id: 9, name: 'Sam' }
  ]
}
 */

in above code consists of an array arr containing objects with id and name properties. There are also two additional arrays, list1 and list2, which contain some IDs.

The goal of the code is to filter the arr array based on the IDs present in list1 and list2, resulting in two sub-arrays: subArr1 and subArr2.

Here's a step-by-step breakdown of the solution:

  • Initialize an empty array subArr1 to store the filtered objects from arr based on the IDs in list1.
  • Use the filter method on the arr array to iterate over each object.
  • For each object, use the includes method on list1 to check if the id - of the object exists in list1.
  • If the object's id is present in list1, it will return true and be included in the subArr1 array.
  • Repeat the same steps to create subArr2 by filtering arr based on the IDs in list2.

After executing the code, subArr1 will contain the objects from arr with IDs 1, 4, and 2, while subArr2 will contain the objects with IDs 3, 8, 9, and 10.

By using the filter method and the includes method, we can easily filter the array of objects based on specific IDs and store the filtered objects in separate sub-arrays.

答案3

得分: 0

以下是您要的中文翻译部分:

这是一个解决方案

```javascript
const filterByIds = (arr, ids) => ids.map(id => arr.find(e => e.id === id)).filter(e => e);

当您不希望在列表中找不到ID时出现undefined时,.filter(e => e)是可选的。

const arr = [
  { id: 1, name: "John" },
  { id: 7, name: "Alice" },
  { id: 10, name: "Ken" },
  { id: 5, name: "Bob" },
  { id: 2, name: "Kevin" },
  { id: 6, name: "Tony" },
  { id: 3, name: "Harry" },
  { id: 4, name: "Kyle" },
  { id: 8, name: "Jane" },
  { id: 9, name: "Sam" },
];
const list1 = [1, 4, 2];
const list2 = [3, 8, 9, 10];

const filterByIds = (arr, ids) => ids.map(id => arr.find(e => e.id === id)).filter(e => e);

const arr1 = filterByIds(arr, list1);
console.log(arr1);

const arr2 = filterByIds(arr, list2);
console.log(arr2);

希望这有所帮助!

英文:

Here's a solution:

const filterByIds = (arr, ids) => ids.map(id => arr.find(e => e.id === id)).filter(e => e);

The .filter(e => e) is optional if you don't want undefined in your list when the id is not found.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const arr = [
  { id: 1, name: &quot;John&quot; },
  { id: 7, name: &quot;Alice&quot; },
  { id: 10, name: &quot;Ken&quot; },
  { id: 5, name: &quot;Bob&quot; },
  { id: 2, name: &quot;Kevin&quot; },
  { id: 6, name: &quot;Tony&quot; },
  { id: 3, name: &quot;Harry&quot; },
  { id: 4, name: &quot;Kyle&quot; },
  { id: 8, name: &quot;Jane&quot; },
  { id: 9, name: &quot;Sam&quot; },
];
const list1 = [1, 4, 2];
const list2 = [3, 8, 9, 10];

const filterByIds = (arr, ids) =&gt; ids.map(id =&gt; arr.find(e =&gt; e.id === id)).filter(e =&gt; e);

const arr1 = filterByIds(arr, list1);
console.log(arr1);

const arr2 = filterByIds(arr, list2);
console.log(arr2);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月15日 12:26:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76479112.html
匿名

发表评论

匿名网友

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

确定