I am trying to set an array as a key value in a JSON object using javascript.
When I set the array value,
console.log(obj["likes"]) displays an array of size 1.
but on the next line console.log(obj) shows that the likes key is an array of size 0.
I have a JSON object that has information about posts.
If no likes exist on a post then that field does not exist in that post's objects.
I am trying to implement a like-dislike update function, where I check if a user has liked a post.
If he hasn't then I append his username to the array of likes else I remove his username.
userID is a global variable that I define at the start of the script tag.
It works if instead of userID, I set a new string like:
obj["likes"] = ["XX"]
This works too (i get an extra space but it atleast logs correctly):
obj["likes"] = [userId+" "]
console.log(obj["likes"])
console.log("Obj:",obj)
But then doing this again does not work!!!!
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
}
}
}
Two problems. 1. That usrId - userId typo randomSoul has mentioned. 2. This line:
likes = likes.push(userId)
The output of likes.push(something)
is a number, the length of the array after push. This line will amount to likes = 1
. Do instead:
likes.push(userId)
push
returns the new length of the array - so this line:
likes = likes.push(userId);
Will be a number, not an array - remove the assignment:
likes.push(userId);
Turnes out I had missed a parenthesis.
But this still does not explain the odd behavior where on one line key value was set and the very next line the output from console.log was different when accessing userId but proper if userId was modified in some way.
Anyway, here's the fixed function:
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)
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.