Ignore the last element of a json

馋奶兔 提交于 2019-12-12 04:57:13

问题


My Json result from the API is as below

Json result:

"Issues": [{
    "Id": null,
    "Key": null,
    "Values": [{
        "Key": "Display Name",
        "Value": "Rya"
      },
      {
        "Key": "UserName",
        "Value": "RH"
      },
      {
        "Key": "Count",
        "Value": "350"
      }
    ]
  },
  {
    "Id": null,
    "Key": null,
    "Values": [{
        "Key": "Display Name",
        "Value": "Mike"
      },
      {
        "Key": "UserName",
        "Value": "ML"
      },
      {
        "Key": "Count",
        "Value": "90"
      }
    ]
  }
]

I did a mapping by doing as below-

.Issues.map(o =>
  o.Values.reduce((acc, {
      Key,
      Value
    }) =>
    (acc[Key] = Value, acc), {}));

The result of the mapping is as below-

{ "Display Name": 'Rya', "UserName" : "RH", value: 350 },
{ "Display Name": 'Mike', "UserName" : "ML", value: 90 }

Desired Result:

{ "Display Name": 'Rya', "UserName" : "RH" },
{ "Display Name": 'Mike', "UserName" : "ML"}

In my requirement, I want to ignore the last element as shown in the desired result.


回答1:


One solution is to add a filter before the reduce to filter out objects with the unwanted Count property.

.Issues.map(o =>
  o.Values
    .filter(({ Key }) => Key !== 'Count')
    .reduce((acc, {
      Key,
      Value
    }) =>
    (acc[Key] = Value, acc), {}));

You could also do this filtering inline during the reduction by not adding objects when Key === 'Count'.

Note: There is no such thing as the last property in a JS object. It is a collection of properties whose actual order is implementation dependent and unreliable. For example, printing your object in different browsers and platforms could give any order whatsoever, nothing guarantees that consistency.




回答2:


The easiest solution is a combination of map, slice and reduce:

json.Issues.map(b => 
    b.Values.slice(0, -1).reduce((c,d) => {
        c[d.Key] = d.Value;
        return c;
}, {}));

Demo:

let j = {
  "Issues": [{
      "Id": null,
      "Key": null,
      "Values": [{
          "Key": "Display Name",
          "Value": "Rya"
        },
        {
          "Key": "UserName",
          "Value": "RH"
        },
        {
          "Key": "Count",
          "Value": "350"
        }
      ]
    },
    {
      "Id": null,
      "Key": null,
      "Values": [{
          "Key": "Display Name",
          "Value": "Mike"
        },
        {
          "Key": "UserName",
          "Value": "ML"
        },
        {
          "Key": "Count",
          "Value": "90"
        }
      ]
    }
  ]
};

let r = j.Issues.map(b =>
  b.Values.slice(0, -1).reduce((c, d) => {
    c[d.Key] = d.Value;
    return c;
  }, {}));
console.log(r);



回答3:


.Issues.map(o => o.Values.reduce((acc, {Key, Value}) => (Key !== 'Count') ? (acc[Key] = Value, acc) : acc, {}));

full code:

const j = {"Issues": [
        {
            "Id": null,
            "Key": null,
            "Values": [
                {
                    "Key": "Display Name",
                    "Value": "Rya"
                },
                {
                    "Key": "UserName",
                    "Value": "RH"
                },
                {
                    "Key": "Count",
                    "Value": "350"
                }
            ]
        },
        {
            "Id": null,
            "Key": null,
            "Values": [
                {
                    "Key": "Display Name",
                    "Value": "Mike"
                },
                {
                    "Key": "UserName",
                    "Value": "ML"
                },
                {
                    "Key": "Count",
                    "Value": "90"
                }
            ]
        }
    ]
    }
    
    const r = j.Issues.map(o => o.Values.reduce((acc, {Key, Value}) => (Key !== 'Count') ? (acc[Key] = Value, acc) : acc, {}));
    
    console.log(JSON.stringify(r, null, 2))



回答4:


Note that in a reduce method, the function is also passed in the current index and the array reduce is being called on. So if you want to ignore the last element of the array, you can do something like:

Issues.map(o => 
   o.Values.reduce((acc, {Key, Value}, idx, arry) => {
     if(idx < arry.length -1)
         acc[Key] = Value;
     return acc;
   }, {}
);



回答5:


To remove the last element regardless of how many items in the array I would favor something like this:

let result = data.Issues.map(issue => {
    let temp = issue.Values;
    temp.splice(-1);
    return temp.reduce((acc, {Key, Value}) => (acc[Key] = Value, acc), {});
});

Here's a fiddle



来源:https://stackoverflow.com/questions/43667526/ignore-the-last-element-of-a-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!