【问题标题】:Insert a new object to array Ramda向数组 Ramda 插入一个新对象
【发布时间】:2020-02-29 23:22:37
【问题描述】:

Ramda 有insert function。但就我而言,我不知道如何钻取对象并将新对象插入数组。另外一个新对象应该在数组的最后一个索引上,我们可以通过stuff["31"].length 获取它。我的尝试太可悲了,所以我决定不展示它们:/

数据模型:

const stuff = {
  "31": [
    {
      "id": "11",
      "title": "ramda heeeelp"
    },
    {
      "id": "12",
      "title": "ramda 123"
    },
    //HERE I WANT TO INSERT A NEW OBJECT - {id: "13", title: "new title"}
  ],
  "33": [
    {
      "id": "3",
      "title": "..."
    }
  ],
  "4321": [
    {
      "id": "1",
      "title": "hello world"
    }
  ]
}

【问题讨论】:

    标签: javascript object functional-programming ramda.js


    【解决方案1】:

    这是对镜头的直接使用,在末尾添加append。

    我会这样做:

    const addObjToGroup = (groupId, newObj, data) => 
      over (lensProp (groupId), append (newObj), data)
    
    const stuff = {"31": [{"id": "11", "title": "ramda heeeelp"}, {"id": "12", "title": "ramda 123"}], "33": [{"id": "3", "title": "..."}], "4321": [{"id": "1", "title": "hello world"}]}
    
    console .log (
      addObjToGroup ('31', {id: "13", title: "new title"}, stuff)
    )
    <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>
    <script>const {over, lensProp, append} = R                           </script>

    【讨论】:

      【解决方案2】:

      您还可以使用evolve 将项目附加到groupId 中的数组中:

      const { evolve, append } = R
      
      const addObjToGroup = (groupId, newObj) => evolve({ [groupId]: append(newObj) })
      
      const stuff = {"31": [{"id": "11", "title": "ramda heeeelp"}, {"id": "12", "title": "ramda 123"}], "33": [{"id": "3", "title": "..."}], "4321": [{"id": "1", "title": "hello world"}]}
      
      const result = addObjToGroup ('31', {id: "13", title: "new title"})(stuff)
      
      console .log (result)
      &lt;script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"&gt;&lt;/script&gt;

      【讨论】:

        【解决方案3】:

        R.useWith 也有帮助!

        const appendTo = R.useWith(R.over, [
          R.lensProp,
          R.append,
          R.identity
        ]);
        
        
        // ---
        const stuff = {
          "31": [
            {
              "id": "11",
              "title": "ramda heeeelp"
            },
            {
              "id": "12",
              "title": "ramda 123"
            },
            //HERE I WANT TO INSERT A NEW OBJECT - {id: "13", title: "new title"}
          ],
          "33": [
            {
              "id": "3",
              "title": "..."
            }
          ],
          "4321": [
            {
              "id": "1",
              "title": "hello world"
            }
          ]
        }
        
        console.log(
          appendTo('31', 'HELLO WORLD', stuff),
        );
        &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"&gt;&lt;/script&gt;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-04-07
          • 2020-02-29
          • 1970-01-01
          • 2021-11-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-28
          相关资源
          最近更新 更多