首页 > 解决方案 > 如何在 JavaScript 中迭代数组

问题描述

这个问题可能是一个简单的迭代,但我陷入了逻辑。

我有一个数据数组,需要根据 id 和 code 进行迭代,并在以下场景中删除数据。

而且,如果所有数据都具有相同的 ID,这将不适用。

** 数据可能只有一个 ID 或两个 ID。不超过两个**

这是案例场景,根据以下数据,

  1. 有两个不同的代码名为“GOOGLE”,具有不同的 id (381, 378) -有效

  2. 有两个不同的代码名为“FACEBOOK”,具有不同的 id (381, 378) -有效

  3. 如果数据中没有不同的 ID(意味着所有数据具有相同的 id),则返回所有数组 -有效

  4. 有一个名为“TWITTER”和“INSTAGRAM”的代码不存在于另一个 id - INVALID中。

在案例 4 中,我想根据案例 4 删除这两个数据。

{ "id" : 378, "code" : "TWITTER", "comment" : "zeeer" }, 

{ "id" : 385, "code" : "INSTAGRAM", "comment" : "hgtff" },

我在下面尝试了一些方法,它适用于案例 1、2 和 4。不适用于案例 3。

const data = [ 
  { "id" : 381, "code" : "GOOGLE", "comment" : "ffff" }, 
  { "id" : 381, "code" : "FACEBOOK", "comment" : "fff" }, 
  { "id" : 378, "code" : "TWITTER", "comment" : "zeeer" }, 
  { "id" : 378, "code" : "GOOGLE", "comment" : "rferer" }, 
  { "id" : 378, "code" : "FACEBOOK", "comment" : "fefehh" },
  { "id" : 385, "code" : "INSTAGRAM", "comment" : "hgtff" },
  { "id" : 378, "code" : "ORKUT", "comment" : "abcd" },
  { "id" : 381, "code" : "ORKUT", "comment" : "abcd" },
];

let result = data.filter(({id,code}) => !!data.find(obj => 
	obj.code === code && obj.id !== id
));

console.log("result", result);

/* Not Working for Case 3 */

const data2 = [ 
  { "id" : 381, "code" : "GOOGLE", "comment" : "ffff" }, 
  { "id" : 381, "code" : "FACEBOOK", "comment" : "fff" }, 
];

let result2 = data2.filter(({id,code}) => !!data2.find(obj => 
	obj.code === code && obj.id !== id
));

console.log("result 2", result2); // Throwing Empty Response, Expected is to return all the two arrays as it has same ID (381).

标签: javascripthtmlecmascript-6

解决方案


无论如何,您的过滤器逻辑都​​没有处理案例#3,因此要实现该功能,您必须添加额外的检查,如果该案例对于给定数据为真。

示例代码,用于检查所有给定数据是否具有相同的“id”

if (data.reduce((prev, cur, _, array) => prev || cur.id !== array[0].id, false) === false) { // Handle case #3 }

编辑:JavaScript 片段。

const findTheItemsYouWant = (data) => {
    // Extra check to check for case #3, where every "id" is the same.
    if (data.reduce((prev, cur, _, array) => prev || cur.id !== array[0].id, false) === false) {
        // All "id"s are the same - return the original input Array.
        return data
    }
    // Otherwise, filter result to only objects that pass the predicate:
    // Array must contain an object with equal "code" and different "id".
    return data.filter(({id, code}) =>
        data.find(obj =>
            obj.code === code && obj.id !== id
        ) !== undefined
    )
}
const data = [ 
    { "id" : 381, "code" : "GOOGLE", "comment" : "ffff" }, 
    { "id" : 381, "code" : "FACEBOOK", "comment" : "fff" }, 
    { "id" : 378, "code" : "TWITTER", "comment" : "zeeer" }, 
    { "id" : 378, "code" : "GOOGLE", "comment" : "rferer" }, 
    { "id" : 378, "code" : "FACEBOOK", "comment" : "fefehh" },
    { "id" : 385, "code" : "INSTAGRAM", "comment" : "hgtff" },
    { "id" : 378, "code" : "ORKUT", "comment" : "abcd" },
    { "id" : 381, "code" : "ORKUT", "comment" : "abcd" },
];
console.log("result", findTheItemsYouWant(data));

const data2 = [ 
    { "id" : 381, "code" : "GOOGLE", "comment" : "ffff" }, 
    { "id" : 381, "code" : "FACEBOOK", "comment" : "fff" }, 
];
console.log("result 2", findTheItemsYouWant(data2));


推荐阅读