使用lodash find从数组中的对象访问值时出现问题

时间:2018-11-01 01:47:56

标签: javascript lodash

所以我有一个像这样的数组

images = [ { "id":8471226, "base64Image":"encodedText" }, ... ]

但是当我尝试访问代码中的encodeText时,却变得未定义

let id = 8480948
console.log( _.find(images, { id }) ); // undefined
console.log(id)
console.log(images)

const images = [{id: 8471226, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8477463, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8479407, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8477961, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8470619, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8471233, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8471239, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8472382, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8473933, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8475149, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8475151, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8475222, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8475663, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8475791, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8476234, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8476399, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8476474, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8476923, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8477355, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8477425, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8477509, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8477520, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8478401, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8479315, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8480002, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{id: 8480948, base64Image: "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"}]
let id = 8480948
console.log( _.find(images, { id }) ); // undefined
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

当我右键单击数组并将其作为全局变量存储在chrome dev工具中时,它是否按预期工作?

这是输出的图像

enter image description here

我在做什么错了?

[{"id": 8471226, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8477463, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8479407, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8477961, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8470619, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8471233, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8471239, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8472382, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8473933, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8475149, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8475151, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8475222, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8475663, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8475791, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8476234, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8476399, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8476474, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8476923, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8477355, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8477425, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8477509, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8477520, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8478401, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8479315, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8480002, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"},
{"id": 8480948, "base64Image": "KAAAAGgAAAAAAAAwvnlmsC+LHdzl+WxWjRcE1u4G8K"}]

2 个答案:

答案 0 :(得分:0)

您的语法似乎100%正确的ES6。

您似乎根本不想在数组中找到记录。

请注意,示例中的ID不同:

8480948
8471226

答案 1 :(得分:0)

我不知道您的代码中的实际问题是什么。但这似乎对我有用。我用过array.find方法。有关Array.find的更多信息是here

<html>
    <body>
        <script>
            var images = [{ "id":8471226, "base64Image":"encodedText" },{"id":234,"base64Image":"somethingelse"}];
            
            console.log("Finding image of id 23:",findImage(234));

            console.log("Finding image of id 8471226:",findImage(8471226));
            
            function findImage(id){
                var found = images.find(function(element) {
                    return element.id == id;
                });
                return found;
            }

            </script>
    </body>
</html>