3

I have multiple objects in array. It has values array with value property in object. when all value properties are empty. I want to remove whole object.

I tried it but i am not able to produce output. Please help me.

var data = [
 {
 "field": "surname",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "given names",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "date of birth",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "25.07.1974",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "05 JUN /JOIN 57",
 "isAvailable": true
 }
 ],
 "status": "passed"
 }
];
 for (let x = data.length-1; x >= 0; x--) {
 let count = 1;
 var dataValueLength = data[x].values.length;
 for (let y = 0; y < data[x].values.length; y++) {
 if (data[x].values[y].value === "") {
 count++;
 }
 }
 if (dataValueLength == count) {
 data.splice(x, 1)
 }
 }
 
 console.log(JSON.stringify(data));

In above scenario, output should be only one object that is "date of birth".

asked Jan 10, 2018 at 14:07
4
  • where in the given data is one object empty? please add an example of what to test and what to delete. Commented Jan 10, 2018 at 14:09
  • Are you talking about "values": [] or where all values.value = "" ? Or what of the two? Commented Jan 10, 2018 at 14:20
  • I am looking for values.value == "" Commented Jan 10, 2018 at 14:30
  • What if date of birth had a third item in values array that had a value property of an empty string? Would you want the date of birth object have only 2 items in values or 3? Commented Jan 10, 2018 at 14:35

7 Answers 7

4

Very close, just one issue (you are initializing count to 1 instead of 0), make it

let count = 0;

Demo

var data = [{
 "field": "surname",
 "values": [{
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "given names",
 "values": [{
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "date of birth",
 "values": [{
 "id": "drivingLicenseFront",
 "value": "25.07.1974",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "05 JUN /JOIN 57",
 "isAvailable": true
 }
 ],
 "status": "passed"
 }
];
for (let x = data.length - 1; x >= 0; x--) {
 let count = 0; //Observe change in this line 
 var dataValueLength = data[x].values.length;
 for (let y = 0; y < data[x].values.length; y++) {
 if (data[x].values[y].value === "") {
 count++;
 }
 }
 if (dataValueLength == count) {
 data.splice(x, 1)
 }
}
console.log(JSON.stringify(data));

answered Jan 10, 2018 at 14:13
Sign up to request clarification or add additional context in comments.

Comments

2

You can use reduce to filter out the items with no values

var data = [
 {
 "field": "surname",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "given names",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "date of birth",
 "values": [ 
 ],
 "status": "passed"
 }
];
var result = data.reduce(function(p,c){
 if(c.values && c.values.length > 0)
 p.push(c);
 return p;
},[]);
console.log(result);

answered Jan 10, 2018 at 14:14

Comments

2

If I understand your question correctly, you can just use filter():

 var filtered = data.filter(item => item.values.some(v => v.value))

This will return an array removing items where the all values.values are false(y).

var data = [
 {
 "field": "surname",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "given names",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "date of birth",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "25.07.1974",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "05 JUN /JOIN 57",
 "isAvailable": true
 }
 ],
 "status": "passed"
 }
];
var filtered = data.filter(item => item.values.some(v => v.value))
console.log(filtered)

answered Jan 10, 2018 at 14:20

Comments

1

You can use Array.prototype.filter and Array.prototype.every

  • filter creates a new array of all element for which the supplied lambda returns True
  • every determines if every element in an array returns True for the supplied lambda

So we want to filter objects where every value (in the values array) is not equal to an empty string

const data1 = 
 data.filter (o =>
 o.values.every (v => v.value !== ''))

Full demo

const data0 = [
 {
 "field": "surname",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "given names",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "date of birth",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "25.07.1974",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "05 JUN /JOIN 57",
 "isAvailable": true
 }
 ],
 "status": "passed"
 }
];
const data1 = 
 data0.filter (f =>
 f.values.every (v => v.value !== ''))
 
console.log (data1)
// [
// {
// "field": "date of birth",
// "values": [
// {
// "id": "drivingLicenseFront",
// "value": "25.07.1974",
// "isAvailable": true
// },
// {
// "id": "passport",
// "value": "05 JUN /JOIN 57",
// "isAvailable": true
// }
// ],
// "status": "passed"
// }
// ]

answered Jan 10, 2018 at 14:24

Comments

0

Start count from 0 and count it correctly :)

https://jsfiddle.net/vLLs4px5/

var data = [
 {
 "field": "surname",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "given names",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "date of birth",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "25.07.1974",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "05 JUN /JOIN 57",
 "isAvailable": true
 }
 ],
 "status": "passed"
 }
];
 for (let x = data.length-1; x >= 0; x--) {
 var count = 0;
 var dataValueLength = data[x].values.length;
 for (let y = 0; y < data[x].values.length; y++) {
 if (data[x].values[y].value == "") {
 count = count + 1;
 }
 }
 if (dataValueLength == count) {
 data.splice(x, 1)
 }
 }
 console.log((data));
answered Jan 10, 2018 at 14:19

Comments

0

You can use forEach

let counter = 0;
data.forEach(function(el) {
 let isEmpty = true;
 let element = el.values
 element.forEach(function(e)) {
 if (e.value != "") {
 isEmpty = false;
 }
 });
 if (!isEmpty) {
 data.splice(counter, 1);
 }
 counter++;
});
answered Jan 10, 2018 at 14:23

Comments

0

Just in case you need this code not to crash on unexpected input or remove any values item that has a falsy value property you can try this:

var data = [
 null,
 undefined,
 {},
 {values:"hello world"},
 {
 "field": "surname",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "given names",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 },
 {
 "field": "date of birth",
 "values": [
 {
 "id": "drivingLicenseFront",
 "value": "25.07.1974",
 "isAvailable": true
 },
 {
 "id": "passport",
 "value": "05 JUN /JOIN 57",
 "isAvailable": true
 },
 {
 "id": "this is gone",
 "value": "",
 "isAvailable": true
 }
 ],
 "status": "passed"
 }
];
data.map(
 data=>
 Object.assign(
 {},
 (data||{}),
 {
 values:Array.from((data&&data.values)||[])
 .filter(
 value=>
 (value&&value.value)
 )
 }
 )
)
.filter(
 data=>
 data.values.length!==0
)
answered Jan 10, 2018 at 14:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.