如何将对象添加到数组中?

huangapple go评论111阅读模式
英文:

How to add a object To array?

问题

我有一个包含电影的对象,我试图根据索引添加/修改评论。

let movies = {
  1: { "director": "Robert Wise", "title": "The Sound of Music", "reviews": [{ "username": "user1", "review": "好电影" }, { "username": "user2", "review": "不错的观影" }] },
  2: { "director": "Tony Richardson", "title": "The Loved One", "reviews": [] },
  3: { "director": "David Lean", "title": "Doctor Zhivago", "reviews": [] },
}

我正在尝试找到最佳的方法来做到这一点,任何帮助将不胜感激。

英文:

So I have this object containing movies, and I'm trying to do add/modify a review based on the index.

let movies= {
      1: {"director": "Robert Wise","title": "The Sound of Music ", "reviews": [{"username" : "user1" , "review" : "good movie"} , {"username" : "user2" , "review" : "good watch"} ] },
      2: {"director": "Tony Richardson","title": "The Loved One ", "reviews": [] },
      3: {"director": "David Lean ","title": "Doctor Zhivago", "reviews": [] },
}

I'm trying to find the best way of doing that, any help would be appreciated.

答案1

得分: 1

将"reviews"条目更改为数组,而不是对象(这是您在电影2和3中的情况)

然后就很简单了

let movies = {
    1: {
        "director": "Robert Wise",
        "title": "The Sound of Music",
        "reviews": [
            { "username": "user1", "review": "好电影" },
            { "username": "user2", "review": "好看" }
        ]
    },
    2: {
        "director": "Tony Richardson",
        "title": "The Loved One",
        "reviews": []
    },
    3: {
        "director": "David Lean",
        "title": "Doctor Zhivago",
        "reviews": []
    }
}

movies[2].reviews.push({ "username": "user2", "review": "相当不错" });

console.log(JSON.stringify(movies, null, 2));
英文:

Make the "reviews" entry be an array, not an object (which is what you have for movies 2 and 3)

Then it is straightforward

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let movies= {
      1: {&quot;director&quot;: &quot;Robert Wise&quot;,&quot;title&quot;: &quot;The Sound of Music &quot;, &quot;reviews&quot;: [{&quot;username&quot; : &quot;user1&quot; , &quot;review&quot; : &quot;good movie&quot;} , {&quot;username&quot; : &quot;user2&quot; , &quot;review&quot; : &quot;good watch&quot;} ] },
      2: {&quot;director&quot;: &quot;Tony Richardson&quot;,&quot;title&quot;: &quot;The Loved One &quot;, &quot;reviews&quot;: [] },
      3: {&quot;director&quot;: &quot;David Lean &quot;,&quot;title&quot;: &quot;Doctor Zhivago&quot;, &quot;reviews&quot;: [] },
}


movies[2].reviews.push({&quot;username&quot;:&quot;user2&quot;,&quot;review&quot;:&quot;quite good&quot;})


console.log(JSON.stringify(movies,null,2))

<!-- end snippet -->

答案2

得分: 0

要添加或修改基于索引的评论,您可以使用以下代码:

// 设置您要添加/修改评论的电影索引
let index = 1;

// 定义新评论
let newReview = { "username": "user3", "review": "great movie!" };

// 如果电影已经有评论,将新评论添加到现有评论数组中
if (movies[index]["reviews"]) {
  movies[index]["reviews"].push(newReview);
} 
// 如果电影还没有评论,创建一个新数组并将新评论分配给电影的评论属性
else {
  movies[index]["reviews"] = [newReview];
}

此代码首先设置了要为其添加/修改评论的电影索引(在此示例中为索引1)。然后,它定义了一个具有用户名和评论的新评论对象。

然后,代码检查电影是否已经有评论,通过检查电影对象的reviews属性是否为真(即,它不为null、undefined、空等)。如果有评论,它使用push()方法将新评论添加到现有数组中。

如果电影尚未有评论,代码将创建一个新数组,将新评论对象分配给电影对象的reviews属性。

您可以根据需要调整索引变量和newReview对象,以添加或修改不同电影的评论。

英文:

To add or modify a review based on the index, you can use the following code:

// Set the index of the movie you want to add/modify a review
let index = 1;

// Define the new review
let newReview = { &quot;username&quot;: &quot;user3&quot;, &quot;review&quot;: &quot;great movie!&quot; };

// If the movie already has reviews, add the new review to the existing reviews array
if (movies[index][&quot;reviews&quot;]) {
  movies[index][&quot;reviews&quot;].push(newReview);
} 
// If the movie doesn&#39;t have any reviews, create a new array with the new review
else {
  movies[index][&quot;reviews&quot;] = [newReview];
}

This code first sets the index of the movie you want to add/modify a review for (in this case, it's movie with index 1). Then, it defines a new review object with a username and review.

The code then checks if the movie already has reviews by checking if the reviews property of the movie object is truthy (i.e., it's not null, undefined, empty, etc.). If it does, it adds the new review to the existing array using the push() method.

If the movie doesn't have any reviews yet, the code creates a new array with the new review object and assigns it to the reviews property of the movie object.

You can adjust the index variable and newReview object as needed to add or modify reviews for different movies.

huangapple
  • 本文由 发表于 2023年3月7日 20:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定