Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我有一个数据,希望将所有元素映射到其他元素_Javascript_Arrays_Angular_Typescript_List - Fatal编程技术网

Javascript 我有一个数据,希望将所有元素映射到其他元素

Javascript 我有一个数据,希望将所有元素映射到其他元素,javascript,arrays,angular,typescript,list,Javascript,Arrays,Angular,Typescript,List,如果我列一张这样的清单 [ { "clauseId": 1, "clauseName": "cover", "texts": [ { "textId": 1, "text": "hello" } ] }, { "clauseId": 3, "clauseName":

如果我列一张这样的清单

[
    {
        "clauseId": 1,
        "clauseName": "cover",
        "texts": [
            {
                "textId": 1,
                "text": "hello"
            }
        ]
    },
    {
        "clauseId": 3,
        "clauseName": "xyz",
        "texts": [
            {
                "textId": 3,
                "text": "hello Everyone"
            },
            {
                "textId": 4,
                "text": "Some data"
            }
        ]
    }
 {
        "clauseId": 2,
        "clauseName": "joining",
        "texts": [
            {
                "textId": 3,
                "text": "hello1"
            },
            {
                "textId": 4,
                "text": "hello2"
            }
        ]
    }
]
我想要一份新的名单

a=[joining,cover]
请注意,如果我颠倒索引,每个元素的索引都很重要

b=[hello1,hello2,hello]
如果

类似地,如果我以类似的方式交换位置[连接,xyz,封面]


请注意,传入数据中可能有多个clauseName和多个文本。这只是一个演示

您可以使用。映射将使用每个子句名称作为键,并指向一个文本数组作为值。然后,您可以将子句数组映射到查找表(即映射)中每个键的值

见下例:

contarr=[{clauseId:1,clauseName:cover,text:[{textId:1,text:hello}]},{clauseId:3,clauseName:xyz,text:[{textId:3,text:hello Everyone},{textId:4,text:Some data}},{clauseId:2,clauseName:join,text:[{textId:3,text:hello1},{textId:4,text:hello2}]; const子句=[连接,覆盖]; const lut=new Maparr.map{clauseName,text}=> [clauseName,text.map{text}=>text] ; const result=clauses.flatMapkey=>lut.getkey;
console.logresult;由于数据类型上不存在flatmap,因此出现错误string@NickParsons在示例中,我在数组上使用.flatMap,仔细检查是否在数组上使用.flatMap听起来像是在字符串上调用它我检查了它,显示了相同的结果error@Nick Parsons@adityamishra如果不知道你是如何使用它,就很难说出问题所在。我能想到的唯一一件事是,您使用的浏览器不支持.flatMap。但是,如果是这种情况,我希望会出现不同的错误。如果您的浏览器不支持.flatMap,您可以使用Lodash uz.flatMap,我相信您在使用angular时可以访问它-如果不支持,您可以随时安装它。我已经更新了我的答案,使用lodash版本的uz.flatmapThank@Nick Parsons.添加了一个代码片段。我刚刚更新了ts.config.json中的lib。这回答了你的问题吗?
b =[hello,hello1,hello2]
a=[xyz,joining,cover]
b=["hello Everyone","Some data",hello1,hello2,hello]
b=["hello1","hello2","hello Everyone","Some data",hello]