Javascript如何从 map 返回数组

标签 javascript arrays ramda.js

我有以下 Json

   var myjson =   [{

            "files": [
                {
                    "domain": "d",
                    "units": [
                        {
                            "key": "key1",
                            "type": "2"

                  },
                        {
                            "key": "key2",
                            "type": "2"
                  },
                        {
                            "key": "key3",
                            "type": "2"
                  }]

            },

                {
                    "domain": "d1",
                    "units": [
                        {
                            "key": "key11",
                            "type": "2"

                  },
                        {
                            "key": "key12",
                            "type": "2"
                  },
                        {
                            "key": "key13",
                            "type": "2"
                  }]

            }]

            },

        {

            "files": [
                {
                    "domain": "d",
                    "units": [
                        {
    ......

我想从这个 Json 数组创建一个新数组。数组的长度将是此 Json 对象中“单位”的数量。

所以我需要提取“单位”并从父对象中添加一些数据。

units: [{
        domain: "",
        type: "",
        key: ""
    }, {
        domain: "",
        type: "",
        key: ""
    },
    {
        domain: "",
        type: "",
        key: ""
    }
....
    ];

我想我可以做这样的事情:

var res = [];

myjson.forEach(function(row) {
     row.files.forEach(function(tfile) {
         tfile.units.forEach(function(unit) {

            var testEntity = {
                domain: tfile.domain,
                type : unit.type,
                key: unit.key

            };

            res.push(testEntity);

        });
    });
});

但是它很难阅读并且看起来不太好。我想做一些类似的事情:

var RESULT = myjson.map(function(row) {
     return row.files.map(function(tfile) {
        return  tfile.units.map(function(unit) {

            return {
                domain: tfile.domain,
                type : unit.type,
                key: unit.key

            };



        });
    });
});

但这不起作用,而且看起来也不好。有什么办法可以做到这一点吗?也许以更具声明性的方式。希望 Ramda.js 能有所帮助。

一般有什么好的方法可以以可读的方式从任何嵌套 json 中获取数据吗?

实现类似的东西:

nestedjson.findAllOnLastlevel(function(item){

return {
key : item.key,
type: type.key,
domain : item.parent.domain}

});

或者以某种方式展平此 json,以便所有父对象的所有属性都移动到叶子子对象。 myjson.flatten("files.units")

jsbin http://jsbin.com/hiqatutino/edit?css,js,console

非常感谢

最佳答案

这里可以使用的功能是Ramda的R.chain函数而不是R.map。您可以将 R.chain 视为一种使用函数映射列表的方法,该函数返回另一个列表,然后将生成的列表列表展平在一起。

// get a list of all files
const listOfFiles =
  R.chain(R.prop('files'), myjson)

// a function that we can use to add the domain to each unit
const unitsWithDomain =
  (domain, units) => R.map(R.assoc('domain', domain), units)

// take the list of files and add the domain to each of its units
const result =
  R.chain(file => unitsWithDomain(file.domain, file.units), listOfFiles)

如果您想更进一步,那么您也可以使用 R.pipeK这有助于将函数组合在一起,这些函数在每个给定函数之间的行为类似于 R.chain。

// this creates a function that accepts the `myjson` list
// then passes the list of files to the second function
// returning the list of units for each file with the domain attached
const process = pipeK(prop('files'),
                      f => map(assoc('domain', f.domain), f.units))

// giving the `myjson` object produces the same result as above
process(myjson)

关于Javascript如何从 map 返回数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37919826/

相关文章:

javascript - 如何将包含(图像标签和一些文本)的 HTML 代码或数据转换为图像以供下载

java - 为什么会发生这种情况? (使用数组、增量)

c++ - 如何在二进制文件上使用 fread() 将数据读入 std::vector?

functional-programming - curry 函数中的反向参数顺序(ramda js)

javascript - Ramda 小组然后将 sibling 转换为 child

javascript - 在这种情况下如何作曲

javascript - 使用 javascript 自动刷新 div

javascript - 使用 sessionStorage 恢复页面(后退按钮)

javascript - JSlint 警告 `redeclaration of var counter` 是否必要?

python - 如何修复 ZigZag 问题中的 'list index out of range' 错误?