简体   繁体   中英

How to update value in JSON in NodeJS and filter to exclude values?

var data = [{
   "description" : "sample",
   "link" : "mylink",
   "id" :"1",
   "deviceName":"mydevice1"
},
{
   "description" : "sample",
   "link" : "mylink",
   "id" :"1",
   "deviceName":"mydevice1"
},
{
   "description" : "sample",
   "link" : "mylink",
   "id" :"",
   "deviceName":""
}]

How do i update deviceName where it is empty that is "deviceName"="" to novaluespecified in my JSON object ??

Note

originally it is a huge array so i can't do it for 1 single index

Also i need to filter out all devices named device1 and NOT include that in my final JSON i searched around and i found .filter for including but could not find a function for excluding something

EDIT

I would prefer not to use jquery and if possible do it with NodeJS fully.. any help is appreciated :)

and i don't mind using jquery if its absolutely required

You can try below Hope it helps.First I have used Array.map to transform the value where deviceName is empty string .Next I have used Array filter with indexOF to filter out the values.

 var data = [{ "description": "sample", "link": "mylink", "id": "1", "deviceName": "mydevice1" }, { "description": "sample", "link": "mylink", "id": "1", "deviceName": "mydevice1" }, { "description": "sample", "link": "mylink", "id": "", "deviceName": "" } ] let _res = data.map(ele => { if (ele.deviceName == "") { ele.deviceName = "novaluespecified" } return ele; }) console.log(_res) // filtering out device1 var data2 = [{ "description": "sample", "link": "mylink", "id": "1", "deviceName": "mydevice1" }, { "description": "sample", "link": "mylink", "id": "1", "deviceName": "mydevice1" }, { "description": "sample", "link": "mylink", "id": "", "deviceName": "" } ] let _res1 = data2.filter(ele => { if (ele.deviceName.indexOf('device1') == -1) { return ele; } }) console.log(_res1)

you can try it like this:

var updatedData = data.map(item => item.deviceName == '' ? { ...item, deviceName: 'novaluespecified'} : {...item} )

output:

[ { description: 'sample',
    link: 'mylink',
    id: '1',
    deviceName: 'mydevice1' },
  { description: 'sample',
    link: 'mylink',
    id: '1',
    deviceName: 'mydevice1' },
  { description: 'sample',
    link: 'mylink',
    id: '',
    deviceName: 'novaluespecified' } ]

To filter you can do something like this:

data.filter(item => item.deviceName !== 'device1')

You can use the function reduce for generating the desired output in only one loop.

 let data = [{"description" : "sample","link" : "mylink","id" :"1","deviceName":"mydevice1"},{"description" : "sample","link" : "mylink","id" :"1","deviceName":"mydevice1"},{"description" : "sample","link" : "mylink","id" :"","deviceName":""}], result = data.reduce((a, {deviceName, ...rest}) => { if(deviceName !== "mydevice1") a.push(Object.assign({}, rest, {deviceName: deviceName.trim() === "" ? "novaluespecified" : deviceName})); return a; } , []); console.log(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM