奇怪的Javascript对象和的indexOf数组 [英] Javascript oddness with array of objects and indexOf

查看:215
本文介绍了奇怪的Javascript对象和的indexOf数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不太抓什么是怎么回事。鉴于阵列(ARR):

Not quite grasping what's going on here. Given the array (arr):

[
    {
        "first_name": "Dan",
        "last_name": "Woodson",
        "id": 1
    },
    {
        "first_name": "Jen",
        "last_name": "Woodson",
        "id": 2
    },
    {
        "first_name": "Yoshi",
        "last_name": "Woodson",
        "id": 3
    }
]

和对象(OBJ):

{
    "first_name": "Yoshi",
    "last_name": "Woodson",
    "id": 3
}

为什么会 arr.indexOf(OBJ)收益 1 (尤其是因为我检索的对象从数组使用它的'身份证'早些时候在功能参数)?

Why would arr.indexOf(obj) return -1 (especially since I retrieved the object from the array using it's 'id' parameter earlier in the function)?

推荐答案

Array.indexOf()如果所提供的对象的完全只能在对象的工作同一个对象,放在

Array.indexOf() will only work on objects if the supplied object is exactly the same object you put in.

这是确切的复制的不足,它是的完全相同的对象的,即必须有数组中的某个对象,这样:

An exact copy is insufficient, it has to be the exact same object, i.e. there must be some object in the array such that:

arr[i] === obj

您需要向您展示如何获取的对象。

You need to show how you retrieved the object.

这篇关于奇怪的Javascript对象和的indexOf数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆