0

I have json response and I want to remove few object key values from it and store the edited response on other part so that I can use again.

I know by using simple javascript, but I don't have any idea in angularjs.

Json response

{
    "$id": "1",
    "XYZ": [],
    "ABC": [
        {
            "$id": "41",
            "ID": 1,
            "Order": 0,
            "Delay": 0,
            "Name": "abc",
            "Count": "9",
            "Storage": 3,
            "Groups": []
        }
    ],
    "Projected": 2019
}

Now from this Json file I want to filter out

"$id": "41","ID": 1,"Order": 0, "Delay": 0, "Groups": [], "Name": "abc"

So my new json structure will be like this which I want to store:

{
    "$id": "1",
    "XYZ": [],
    "ABC": [
        {
            "Count": "9",
            "Storage": 3
        }
    ],
    "Projected": 2019
}

Any method to achieve ?

4
  • This has nothing to do with angularJS , you can use Javascript. Use Object.delete or some other way around Commented May 18, 2018 at 8:41
  • Angular don't have a particular method for this. Except angular.forEach. You must use vanilla js. Commented May 18, 2018 at 8:42
  • @ShashankVivek I got you , but it has something to do as I want to filter it out as soon as data is loaded from service before loading in controller. tried with object.delete didn't worked out. Commented May 18, 2018 at 8:45
  • 1
    The answer of @Lorenz will help you. Let us know if you need more explanation Commented May 18, 2018 at 8:49

3 Answers 3

4

You don't need some magic angular stuff. You can just use plain old JavaScript. My apporach iterates through all the items in the ABC array and deletes all properties defined in the props array. Note, that this actively modifies the ABC array items.

const obj = {
    "$id": "1",
    "XYZ": [],
    "ABC": [
        {
            "$id": "41",
            "ID": 1,
            "Order": 0,
            "Delay": 0,
            "Name": "abc",
            "Count": "9",
            "Storage": 3,
            "Groups": []
        }
    ],
    "Projected": 2019
}

// Now from this Json file I want to filter out

const props = ["$id", "ID", "Order", "Delay", "Groups", "Name"];
props.forEach(prop => {
    obj.ABC.forEach(abc => {
      delete abc[prop];
    });
});

console.log(obj);

Sign up to request clarification or add additional context in comments.

2 Comments

I tried this part in my code props.forEach(prop => { $scope.list.ABC.forEach(ABC=> { delete ABC[props]; }); its is showing error :- typeError: Cannot read property 'forEach' of undefined at props.forEach.prop
You should take care which variables you want to delete. You want to delete all variables declared in props, iteratered by forEach, assigning the name prop. So you want something like props.forEach(prop => { $scope.list.ABC.forEach(abc=> { delete abc[prop]; });. Take care: delete abc[prop], not delete abc[props]. If that does not work, would you mind givin us a complete code example?
2

An alternative to the other solutions. If we have a variable called json. This method is simple

let len = json.ABC.length;
for (let i=0;i<len;i++){
    delete json.ABC[i].$id;
    delete json.ABC[i].ID;
    delete json.ABC[i].Order;
    delete json.ABC[i].Delay;
    delete json.ABC[i].Groups;
    delete json.ABC[i].Name;
}

Comments

1

try this

let json = {
    "$id": "1",
    "XYZ": [],
    "ABC": [
        {
            "$id": "41",
            "ID": 1,
            "Order": 0,
            "Delay": 0,
            "Name": "abc",
            "Count": "9",
            "Storage": 3,
            "Groups": []
        }
    ],
    "Projected": 2019
};

json["ABC"] = json["ABC"].map(obj => ({
  "Count": obj["Count"],
  "Storage": obj["Storage"]
}));
// or dynamic way
let keepkeys = ["Storage", "Count"];
json["ABC"] = json["ABC"].map(obj => {
  let newObj = {};
   keepkeys.forEach(key => newObj[key] = obj[key]);
  return newObj;
});
console.log(json)

1 Comment

Let me try this.

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.