如何遍历对象数组以查找用户的ID?

时间:2020-07-04 10:46:38

标签: javascript

如何在JavaScript中循环浏览此JSON并检查用户ID是否存在?这些数据来自图像注释集合,我将需要防止用户编辑其他人的注释。

在此示例中,只说我正在寻找的用户ID是K2Lb1R7owqR9BYmpJAJzrg6w1s92键中的creator:

例如,我尝试使用arr.includes(),但不适用于我。

我的预期输出是多少?我想现在我只需要担心用户ID是否存在,如果存在,则返回false或true。

creator对象将始终出现在body内部。

谢谢您的帮助。

[{
    "body": [{
        "created": "2020-07-03T11:49:32.058Z",
        "purpose": "commenting",
        "value": "don't move me",
        "creator": {
            "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
            "name": "testme"
        },
        "type": "TextualBody"
    }, {
        "purpose": "tagging",
        "value": "move",
        "creator": {
            "name": "testme",
            "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
        },
        "type": "TextualBody",
        "created": "2020-07-03T11:49:30.850Z"
    }],
    "id": "IRCJPsz4NKkiuhVLE1hR",
    "photoDocId": "92wNwz2aaqy7CWf3mGo1",
    "@context": "http://www.w3.org/ns/anno.jsonld",
    "type": "Annotation",
    "target": {
        "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0",
        "selector": {
            "conformsTo": "http://www.w3.org/TR/media-frags/",
            "type": "FragmentSelector",
            "value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344"
        }
    }
}, {
    "body": [{
        "purpose": "commenting",
        "type": "TextualBody",
        "creator": {
            "name": "doss",
            "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
        },
        "value": "anyone ever climb this mountain top?",
        "created": "2020-07-03T10:57:40.590Z"
    }, {
        "creator": {
            "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92",
            "name": "doss"
        },
        "purpose": "tagging",
        "value": "ridge",
        "type": "TextualBody",
        "created": "2020-07-03T10:57:39.351Z"
    }, {
        "value": "i did in 2005",
        "created": "2020-07-03T10:59:45.318Z",
        "type": "TextualBody",
        "creator": {
            "name": "testme",
            "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
        },
        "purpose": "commenting"
    }, {
        "type": "TextualBody",
        "created": "2020-07-03T10:59:43.966Z",
        "purpose": "tagging",
        "value": "testme",
        "creator": {
            "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
            "name": "testme"
        }
    }, {
        "type": "TextualBody",
        "purpose": "replying",
        "value": "test",
        "creator": {
            "name": "doss",
            "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
        },
        "created": "2020-07-03T11:39:18.860Z"
    }],
    "type": "Annotation",
    "@context": "http://www.w3.org/ns/anno.jsonld",
    "photoDocId": "92wNwz2aaqy7CWf3mGo1",
    "target": {
        "selector": {
            "value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156",
            "conformsTo": "http://www.w3.org/TR/media-frags/",
            "type": "FragmentSelector"
        },
        "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"
    },
    "id": "IlI7SRjFm8qohmHDLBw2"
}]

2 个答案:

答案 0 :(得分:0)

您可以使用flatMap来获取数组中的平面数据,然后使用some来找出该值是否存在:

var array=[{ "body": [{ "created": "2020-07-03T11:49:32.058Z", "purpose": "commenting", "value": "don't move me", "creator": { "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2", "name": "testme" }, "type": "TextualBody" }, { "purpose": "tagging", "value": "move", "creator": { "name": "testme", "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2" }, "type": "TextualBody", "created": "2020-07-03T11:49:30.850Z" }], "id": "IRCJPsz4NKkiuhVLE1hR", "photoDocId": "92wNwz2aaqy7CWf3mGo1", "@context": "http://www.w3.org/ns/anno.jsonld", "type": "Annotation", "target": { "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0", "selector": { "conformsTo": "http://www.w3.org/TR/media-frags/", "type": "FragmentSelector", "value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344" } }}, { "body": [{ "purpose": "commenting", "type": "TextualBody", "creator": { "name": "doss", "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92" }, "value": "anyone ever climb this mountain top?", "created": "2020-07-03T10:57:40.590Z" }, { "creator": { "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92", "name": "doss" }, "purpose": "tagging", "value": "ridge", "type": "TextualBody", "created": "2020-07-03T10:57:39.351Z" }, { "value": "i did in 2005", "created": "2020-07-03T10:59:45.318Z", "type": "TextualBody", "creator": { "name": "testme", "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2" }, "purpose": "commenting" }, { "type": "TextualBody", "created": "2020-07-03T10:59:43.966Z", "purpose": "tagging", "value": "testme", "creator": { "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2", "name": "testme" } }, { "type": "TextualBody", "purpose": "replying", "value": "test", "creator": { "name": "doss", "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92" }, "created": "2020-07-03T11:39:18.860Z" }], "type": "Annotation", "@context": "http://www.w3.org/ns/anno.jsonld", "photoDocId": "92wNwz2aaqy7CWf3mGo1", "target": { "selector": { "value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156", "conformsTo": "http://www.w3.org/TR/media-frags/", "type": "FragmentSelector" }, "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0" }, "id": "IlI7SRjFm8qohmHDLBw2"}];

var result = array.flatMap(elem=>elem.body).some(user=>user.creator.id==='K2Lb1R7owqR9BYmpJAJzrg6w1s92');

console.log(result);

some将返回truefalse,这是您在这里需要的。

我希望这会有所帮助。

答案 1 :(得分:0)

您可以使用some函数:

const data = [{"body":[{"created":"2020-07-03T11:49:32.058Z","purpose":"commenting","value":"don't move me","creator":{"id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2","name":"testme"},"type":"TextualBody"},{"purpose":"tagging","value":"move","creator":{"name":"testme","id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2"},"type":"TextualBody","created":"2020-07-03T11:49:30.850Z"}],"id":"IRCJPsz4NKkiuhVLE1hR","photoDocId":"92wNwz2aaqy7CWf3mGo1","@context":"http://www.w3.org/ns/anno.jsonld","type":"Annotation","target":{"source":"https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0","selector":{"conformsTo":"http://www.w3.org/TR/media-frags/","type":"FragmentSelector","value":"xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344"}}},{"body":[{"purpose":"commenting","type":"TextualBody","creator":{"name":"doss","id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92"},"value":"anyone ever climb this mountain top?","created":"2020-07-03T10:57:40.590Z"},{"creator":{"id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92","name":"doss"},"purpose":"tagging","value":"ridge","type":"TextualBody","created":"2020-07-03T10:57:39.351Z"},{"value":"i did in 2005","created":"2020-07-03T10:59:45.318Z","type":"TextualBody","creator":{"name":"testme","id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2"},"purpose":"commenting"},{"type":"TextualBody","created":"2020-07-03T10:59:43.966Z","purpose":"tagging","value":"testme","creator":{"id":"0qbvzjI3llhyIrKVO6PZxcZUSiI2","name":"testme"}},{"type":"TextualBody","purpose":"replying","value":"test","creator":{"name":"doss","id":"K2Lb1R7owqR9BYmpJAJzrg6w1s92"},"created":"2020-07-03T11:39:18.860Z"}],"type":"Annotation","@context":"http://www.w3.org/ns/anno.jsonld","photoDocId":"92wNwz2aaqy7CWf3mGo1","target":{"selector":{"value":"xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156","conformsTo":"http://www.w3.org/TR/media-frags/","type":"FragmentSelector"},"source":"https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"},"id":"IlI7SRjFm8qohmHDLBw2"}]

console.log(data.some(({body}) => body.some(({creator}) => creator.id === 'K2Lb1R7owqR9BYmpJAJzrg6w1s92')));