如何在JavaScript对象中将数组设置为键值?

时间:2019-04-07 10:09:33

标签: javascript html arrays json

我正在尝试使用JavaScript将数组设置为JSON对象中的键值。

设置数组值时,

console.log(obj [“ likes”])显示大小为1的数组。

但在下一行 console.log(obj)显示Likes键是大小为0的数组。


我有一个JSON对象,其中包含有关帖子的信息。

如果帖子中没有喜欢,则该字段在该帖子的对象中不存在。

我正在尝试实现一个不喜欢的更新功能,该功能用于检查用户是否喜欢帖子。

如果他还没有,那么我会将他的用户名附加到其他点赞列表中,否则我将删除他的用户名。

userID是我在脚本标记开头定义的全局变量。

如果我设置一个新的字符串而不是userID,它会起作用:

obj["likes"] = ["XX"]

这也可以工作(我有一个额外的空间,但它至少可以正确记录日志):

obj["likes"] = [userId+" "]
console.log(obj["likes"])
console.log("Obj:",obj)

但是再做一次是行不通的!!!!

let arr = [" "+userId]
console.log(arr)
arr[0] = arr[0].trim()
console.log(arr)
obj["likes"] = arr
console.log("Obj:",obj)
function saveLikeDislike(url, action) {
        for (i = 0; i < crawledUrlsData.length; i += 1) {

            if (typeof crawledUrlsData[i] === "object") {

                var obj = crawledUrlsData[i]

                if (url === obj["url"]) {

                    if (action === "like") {

                        if ("likes" in obj) {
                            likes = obj["likes"]
                            if (likes.includes(userId)) {
                                likes = likes.filter(id => id != userId)
                            } else {
                                likes.push(userId)
                            }
                            obj["likes"] = likes
                        } else {
                            var id = window.userId
                            console.log(userId)

                            obj["likes"] = [id]
                            console.log(obj["likes"])
                            console.log("Obj:",obj)
                        }

                        if ("dislikes" in obj) {
                            var dislikes = obj["dislikes"]

                            if (dislikes.includes(userId)) {
                                dislikes = dislikes.filter(id => id != userId)
                                obj["dislikes"] = dislikes
                            }
                        }

                    } else {

                        if ("dislikes" in obj) {
                            dislikes = obj["dislikes"]
                            if (dislikes.includes(userId)) {
                                dislikes = dislikes.filter(id => id != userId)
                            } else {
                                dislikes.push(userId)
                            }
                            obj["dislikes"] = dislikes
                        } else 
                            obj["dislikes"] = [dislikes]
                        }

                        if ("likes" in obj) {
                            var likes = obj["likes"]

                            if (likes.includes(userId)) {
                                likes = likes.filter(id => id != userId)
                                obj["likes"] = likes
                            }
                        }
                    }

                    crawledUrlsData[i] = obj
                    console.log(obj["likes"])

                    renderData()
                    return
                }
        }
    }

console output

3 个答案:

答案 0 :(得分:0)

两个问题。 1.提到usrId-userId错字。 2.此行:

likes = likes.push(userId)

likes.push(something)的输出是一个数字,即推送后数组的长度。此行总计为likes = 1。改为:

likes.push(userId)

答案 1 :(得分:0)

push返回数组的新长度-所以这一行:

likes = likes.push(userId);

将是数字,而不是数组-删除赋值:

likes.push(userId);

答案 2 :(得分:0)

结果证明我错过了括号。

但是,这仍然不能解释在访问userId时设置一行键值和在下一行console.log输出不同的奇怪行为,但是如果以某种方式修改了userId则正确。

无论如何,这是固定功能:

function saveLikeDislike(url, action) {
    for (i = 0; i < crawledUrlsData.length; i += 1) {

        if (typeof crawledUrlsData[i] === "object" && crawledUrlsData[i]["url"] == url) {

            var obj = crawledUrlsData[i]

            if (url === obj["url"]) {

                if (action === "like") {

                    if ("likes" in obj) {
                        console.log("likes in obj")
                        likes = obj["likes"]
                        if (likes.includes(userId)) {
                            likes = likes.filter(id => id != userId)
                        } else {
                            likes.push(userId)
                        }
                        obj["likes"] = likes
                    } else {

                        obj.likes = [userId]
                        console.log("Obj:",obj)
                    }

                    if ("dislikes" in obj) {
                        var dislikes = obj["dislikes"]
                        console.log("Dislikes: ",dislikes)
                        if (dislikes.includes(userId)) {
                            dislikes = dislikes.filter(id => id != userId)
                            obj["dislikes"] = dislikes
                        }
                    }

                } else if (action === "dislike"){

                    if ("dislikes" in obj) {
                        dislikes = obj["dislikes"]
                        if (dislikes.includes(userId)) {
                            dislikes = dislikes.filter(id => id != userId)
                        } else {
                            dislikes.push(userId)
                        }
                        obj["dislikes"] = dislikes
                    } else {
                        obj["dislikes"] = [userId]
                    }
                    if ("likes" in obj) {
                        var likes = obj["likes"]
                        console.log("ID: ",userId)
                        if (likes.includes(userId)) {
                            likes = likes.filter(id => id != userId)
                            obj["likes"] = likes
                        }
                    }
                }    
            }

                crawledUrlsData[i] = obj

                linkTreeRef.set(crawledUrlsData)
        }
    }
}