0

Hi Here i am having a object that is returned from api.

let ApiData1 = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 10,
                "stateId": 10,
                "name": "Tamil Nadu",
                "code": "TN"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 11,
                "stateId": 11,
                "name": "Karnataka",
                "code": "KA"
            }
        },
        null,
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 12,
                "stateId": 12,
                "name": "Pondicherry",
                "code": "PY"
            }
        },
        null,
          {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 1,
                "stateId": 1,
                "name": "Delhi",
                "code": "DL"
            }
        }

    ]
}

In this data , in ApiData1.features . there are some null values retuned . I need to remove this null values and has to return like this.

OutPut

{
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 10,
                "stateId": 10,
                "name": "Tamil Nadu",
                "code": "TN"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 11,
                "stateId": 11,
                "name": "Karnataka",
                "code": "KA"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 12,
                "stateId": 12,
                "name": "Pondicherry",
                "code": "PY"
            }
        },
          {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 1,
                "stateId": 1,
                "name": "Delhi",
                "code": "DL"
            }
        }

    ]
}

is anyway to achieve this by passing this ApiData into a function and return like this. Could u please help me with this.

Thanks in advance

4
  • is this related to the question you asked 32 minutes ago? stackoverflow.com/questions/61965838/… Commented May 23, 2020 at 0:55
  • kind of, but it is not same. Commented May 23, 2020 at 0:56
  • 1
    What have you tried so far? Might take a look at the Array.filter method Commented May 23, 2020 at 0:58
  • ApiData1.features = ApiData1.features.filter(o => o) Commented May 23, 2020 at 1:00

3 Answers 3

2

Try this , it should help get rid of nulls

ApiData1.features = ApiData1.features.filter(ob => ob !==null)
Sign up to request clarification or add additional context in comments.

4 Comments

Are you sure you can assign the value like this? Will ApiData1.features be recognized as a valid variable name?
Yes, you can . We dont want to change the full ApiData1 , we need to filter out the values from the features array only.
@ManirajMurugan ApiData1.features is NOT a variable name, features is a property of the object in variable ApiData1
@JaromandaX, Thanks for mentioning it, I can understand now that property value is alone getting changed.. My bad understanding..
1

You can use ES6 spread operator and .filter() to filter the null data, here is a working snippet:

let ApiData1 = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 10,
                "stateId": 10,
                "name": "Tamil Nadu",
                "code": "TN"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 11,
                "stateId": 11,
                "name": "Karnataka",
                "code": "KA"
            }
        },
        null,
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 12,
                "stateId": 12,
                "name": "Pondicherry",
                "code": "PY"
            }
        },
        null,
          {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 1,
                "stateId": 1,
                "name": "Delhi",
                "code": "DL"
            }
        }

    ]
}

let newData = {
  ...ApiData1,
  features: ApiData1.features.filter((el) => el)
}

console.log(newData);

Comments

0

Added flavor of Imperative style of coding.

  • Used forEach loop
  • In JS object/Arrays are pass by ref.
  • Updates ApiData1 features data using [...spreadOperator]
  var ApiData1 = {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 10,
                "stateId": 10,
                "name": "Tamil Nadu",
                "code": "TN"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 11,
                "stateId": 11,
                "name": "Karnataka",
                "code": "KA"
            }
        },
        null,
        {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 12,
                "stateId": 12,
                "name": "Pondicherry",
                "code": "PY"
            }
        },
        null,
          {
            "type": "Feature",
            "geometry": {
                "type": "MultiPolygon"
            },
            "properties": {
                "color": 1,
                "id": 1,
                "stateId": 1,
                "name": "Delhi",
                "code": "DL"
            }
        }

    ]
};


var features =[];
ApiData1.features.forEach(feature => feature ? features.push(feature): null);
ApiData1['features'] = [...features];
console.log(ApiData1);

Comments

Your Answer

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.