0
Follow
2
View

How do I find the same object as arr_1 in the arr array

dl280985467 注册会员
2023-02-28 21:25
Hello, I am asked to answer the little assistant, your question has been a little partner to help you answer, thank you for your support and attention to have asked to answer!
PS: Q&A VIP annual card [limited time plus: free IT technical books], for details > > > https://vip.csdn.net/askvip?utm_source=1146287632
dxh6258 注册会员
2023-02-28 21:25

The following answers are quoted from GPT-3 model, please use them wisely:

示例

//使用forEach方法可以循环arr_1,来查找在arr中是否存在与之相同的对象 
let newArr = [];
arr_1.forEach(item => {
  //标记arr_1中遍历到的当前对象是否存在arr中
  let isExisted = false; 
  //查找arr中是否有与之相同的对象
  arr.forEach(item_1 => {
    item_1.details.forEach(item_2 => {
      //如果当前item和item_2完全一致,说明存在与之相同的对象
      if (JSON.stringify(item_2) === JSON.stringify(item)) {
        newArr.push(item_2);
        isExisted = true;
      }
    })
  });
  // 如果数组arr中不存在相同的对象,那么就压入arr_1中遍历到的当前对象
  if (!isExisted) {
    newArr.push(item)
  }
});
  console.log(newArr);

Please accept my answer if it answers your question

dragon4cn 注册会员
2023-02-28 21:25

1, the following manual handwritten pro test effective(hope to adopt, thank you!)

img

<script type="text/javascript">
    let arr = [{
        "id": 41,
        "name": "详情一",
        "details": [{
            "index": 1,
            "name": "皮皮",
            "age": 32,
            "address": '南京市'
        }, {
            "index": 1,
            "name": "悠悠",
            "age": 33,
            "address": '琼海市'
        }, {
            "index": 2,
            "name": "涛涛",
            "age": 23,
            "address": '杭州市'
        }, {
            "index": 1,
            "name": "赵三",
            "age": 42,
            "address": '琼海市'
        }, {
            "index": 1,
            "name": "八八",
            "age": 21,
            "address": '北京市'
        }]
    }, {
        "id": 43,
        "name": "详情二",
        "details": [{
            "index": 1,
            "name": "张三",
            "age": 12,
            "address": '上海市'
        }, {
            "index": 1,
            "name": "李四",
            "age": 33,
            "address": '杭州市'
        }, {
            "index": 1,
            "name": "王五",
            "age": 21,
            "address": '南昌市'
        }, {
            "index": 1,
            "name": "赵六",
            "age": 32,
            "address": '南京市'
        }]
    }]

    let arr_1 = [{
        "index": 1,
        "name": "张三",
        "age": 12,
        "address": '上海市浦东新区'
    }, {
        "index": 1,
        "name": "李四",
        "age": 33,
        "address": '杭州市'
    }, {
        "index": 1,
        "name": "王五",
        "age": 21,
        "address": '南昌市'
    }, {
        "index": 1,
        "name": "赵六",
        "age": 32,
        "address": '南京市'
        }]

    // 三重循环法
    var sameArr = [];
    var length = arr.length;
    for (var i = 0; i < length; i++) {
        var dataDetails = arr[i].details;
        var lengthDetails = dataDetails.length;
        for (var j = 0; j < lengthDetails; j++) {
            var dataOne = dataDetails[j];
            for (var l = 0; l < arr_1.length; l++) {
                var dataTwo = arr_1[l];

                // 只判断name相同,则认为是相同的对象
                if (dataOne.name == dataTwo.name) {
                    sameArr.push(dataOne);
                }
            }
        }
    }
    console.log(sameArr);
script>

dandan1980 注册会员
2023-02-28 21:25
< div class = " md_content_show“数据- v - 3967 e397 = " " >

 
    let result = [];
    arr_1.forEach((i, iIndex) => {
        arr.forEach((j, jIndex) => {
            //name,age,address一样判断为同一个人,要缩减判断条件改这里去掉一些条件。
            //比如不比较address用这个: var index = j.details.findIndex(k => k.name == i.name && k.age == i.age);
            var index = j.details.findIndex(k => k.name == i.name && k.age == i.age && k.address == i.address);
            if (index != -1) {//找到,改成需要的操作,这里只是输出位置
                result.push(`arr_1第${iIndex + 1}${i.name}出现在arr第${jIndex + 1}项的details数组第${index + 1}项中`)
            }
        })
    });
    console.log(result)

deathyeah 注册会员
2023-02-28 21:25

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > can use loop through the array arr, then compare the details of each element in the array contains arr_1 all objects in the array, Returns the element if it is included, or null otherwise. Example code is as follows:

function findArr(arr, arr_1) {
  for (let i = 0; i < arr.length; i++) {
    let details = arr[i].details;
    let found = arr_1.every(obj => {
      return details.some(detail => {
        return detail.name === obj.name && detail.age === obj.age && detail.address === obj.address;
      });
    });
    if (found) {
      return arr[i];
    }
  }
  return null;
}

let result = findArr(arr, arr_1);
console.log(result);


This function returns the elements that contain all the objects in the arr_1 array, or null if there is no matching element.

cyang2000416 注册会员
2023-02-28 21:25

I don't know exactly what effect to achieve, here is to print out the position of the arr_1 item in arr and details


<script>

    let arr = [{
        "id": 41,
        "name": "详情一",
        "details": [{
            "index": 1,
            "name": "皮皮",
            "age": 32,
            "address": '南京市'
        }, {
            "index": 1,
            "name": "悠悠",
            "age": 33,
            "address": '琼海市'
        }, {
            "index": 2,
            "name": "涛涛",
            "age": 23,
            "address": '杭州市'
        }, {
            "index": 1,
            "name": "赵三",
            "age": 42,
            "address": '琼海市'
        }, {
            "index": 1,
            "name": "八八",
            "age": 21,
            "address": '北京市'
        }]
    }, {
        "id": 43,
        "name": "详情二",
        "details": [{
            "index": 1,
            "name": "张三",
            "age": 12,
            "address": '上海市'
        }, {
            "index": 1,
            "name": "李四",
            "age": 33,
            "address": '杭州市'
        }, {
            "index": 1,
            "name": "王五",
            "age": 21,
            "address": '南昌市'
        }, {
            "index": 1,
            "name": "赵六",
            "age": 32,
            "address": '南京市'
        }]
    }]

    let arr_1 = [{
        "index": 1,
        "name": "张三",
        "age": 12,
        "address": '上海市浦东新区'
    }, {
        "index": 1,
        "name": "李四",
        "age": 33,
        "address": '杭州市'
    }, {
        "index": 1,
        "name": "王五",
        "age": 21,
        "address": '南昌市'
    }, {
        "index": 1,
        "name": "赵六",
        "age": 32,
        "address": '南京市'
        }]

    let result = [];
    arr_1.forEach((i, iIndex) => {
        arr.forEach((j, jIndex) => {
            var index = j.details.findIndex(k => k.name == i.name && k.age == i.age && k.address == i.address);//根据name,age,address一样判断为同一个人
            if (index != -1) {
                result.push(`arr_1第${iIndex + 1}${i.name}出现在arr第${jIndex + 1}项的details数组第${index + 1}项中`)
            }
        })
    });
    console.log(result)
script>

d911deng000 注册会员
2023-02-28 21:25
< div class = " md_content_show“数据- v - 3967 e397 = " " >

< div class = " aw-list-img " > img