javascript - 在每个数组元素后追加 '|'

标签 javascript json angular typescript

我有这个 JSON,我想做的是制作类似 Action |冒险|喜剧的类型

{
        "id": 1,
        "key": "deadpool",
        "name": "Deadpool",
        "description": "A former Special Forces operative turned mercenary is subjected to a rogue experiment that leaves him with accelerated healing powers, adopting the alter ego Deadpool.",
        "genres": [
            "action",
            "adventure",
            "comedy"
        ],
        "rate": 8.6,
        "length": "1hr 48mins",
        "img": "assets/images/movie-covers/deadpool.jpg"
    },
    {
        "id": 2,
        "key": "we-are-the-millers",
        "name": "We're the Millers",
        "description": "A veteran pot dealer creates a fake family as part of his plan to move a huge shipment of weed into the U.S. from Mexico.",
        "genres": [
            "adventure",
            "comedy",
            "crime"
        ],
        "rate": 7,
        "length": "1hr 50mins",
        "img": "assets/images/movie-covers/we-are-the-millers.jpg"
    }

我的组件代码片段

           data => {
                this.movies = data;
                var tempArr= data
                var genres;
               let genresWithPipe=[];          
                let len= tempArr.length;
                for(var i=0; i<len; i++){
                    genres=tempArr[i].genres+'|';
                   // console.log(tempArr[i].genres)
                 for(var j=0;j<genres.length; j++){
                    if(j<genres.length-1)
                    genres[j]= genres[j]+'|';
                    console.log(genres,data);
                    //genresWithPipe=genres;
                }
                }
                console.log(this.movies,genres)


            }

我曾尝试在我的组件中借助 for 循环来完成它,但是当我在 *ngFor 的帮助下在 html 中显示它时,因为它是一个局部变量,所以它不会显示。如果我将数组值存储在全局变量中,则该变量仅存储最后一个数组。

最佳答案

您可以使用 map 方法来实现您的要求并获得更清晰的解决方案。

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

此外,您可以使用 join 方法来获取结构 action|adventure|comedy| 分隔符。

let array=[{ "id": 1, "key": "deadpool", "name": "Deadpool", "description": "A former Special Forces operative turned mercenary is subjected to a rogue experiment that leaves him with accelerated healing powers, adopting the alter ego Deadpool.", "genres": [ "action", "adventure", "comedy" ], "rate": 8.6, "length": "1hr 48mins", "img": "assets/images/movie-covers/deadpool.jpg" }, { "id": 2, "key": "we-are-the-millers", "name": "We're the Millers", "description": "A veteran pot dealer creates a fake family as part of his plan to move a huge shipment of weed into the U.S. from Mexico.", "genres": [ "adventure", "comedy", "crime" ], "rate": 7, "length": "1hr 50mins", "img": "assets/images/movie-covers/we-are-the-millers.jpg" }];       
 array=array.map(function(item){
    item.genres=item.genres.join('|');
    return item;
 });
 console.log(array);

关于javascript - 在每个数组元素后追加 '|',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46449528/

相关文章:

javascript - 将 Firebase WebApp 源添加到普通 JS 或 JSON 中而不使用 &lt;script src>?

ios - 我如何根据用户的回答为常见问题创建模型?

javascript - 用JS解析JS数组然后传递给PHP

javascript - 平均应用程序。两台服务器同时使用Express、NodeJS、Angular 2

javascript - 触发 OnClick 时对象属性变为 null

javascript - 折叠中使用的模运算符的单位元

javascript - 本地保存数据的问题

javascript - 无法让高阶函数 find 工作

javascript - 在 JavaScript 中使用摩擦力和动量滚动

Angular 路由器 v3 : targeting named outlet