1

hello everyone i have the following issue, i have an array of JSON object like this :

$scope.array_one=[

  {
    "field1": "value 1",
    "field2": "value 2",
    "field3": "value 3",
    "field4": "value 4",
    "field5": "value 5",
    "field6": "value 6",
    "field7": "value 7",
    "field8": "value 8"
  },
  {
    "field1": "value 1",
    "field2": "value 2",
    "field3": "value 3",
    "field4": "value 4",
    "field5": "value 5",
    "field6": "value 6",
    "field7": "value 7",
    "field8": "value 8"
  }

  ...
]

i want to create an new array (from the $scope.array_one) that contains only objects with 3 fields

something like this :

[

  {

    "field2": "value 2",
    "field3": "value 3",
    "field6": "value 6",
    "field8": "value 8"
  },
  {
    "field2": "value 2",
    "field3": "value 3",
    "field6": "value 6",
    "field8": "value 8"
  }

  ...
]

anyone can guide me on how to achieve this, i 'm using Angular js.

Regards !

2
  • Have you tried anything yourself ? Try using Array.filter to achieve this. I think it should be sufficient. Commented Oct 12, 2017 at 9:28
  • @ShivenSinha i want to extract only the field2,field3,field6,field8 from the array_one , something like projection .. Commented Oct 12, 2017 at 9:31

2 Answers 2

2

You could map the wanted parts with a destruction.

var $scope = { array_one: [{ field1: "value 1", field2: "value 2", field3: "value 3", field4: "value 4", field5: "value 5", field6: "value 6", field7: "value 7", field8: "value 8" }, { field1: "value 1", field2: "value 2", field3: "value 3", field4: "value 4", field5: "value 5", field6: "value 6", field7: "value 7", field8: "value 8" }] },
    result = $scope.array_one.map(
        ({ field2, field3, field6, field8 }) => ({ field2, field3, field6, field8 })
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

Comments

1

Using Array#Map() to create a new array by returning a new object

let array_one = [
  {
    "field1": "value 1",
    "field2": "value 2",
    "field3": "value 3",
    "field4": "value 4",
    "field5": "value 5",
    "field6": "value 6",
    "field7": "value 7",
    "field8": "value 8"
  },
  {
    "field1": "value 1",
    "field2": "value 2",
    "field3": "value 3",
    "field4": "value 4",
    "field5": "value 5",
    "field6": "value 6",
    "field7": "value 7",
    "field8": "value 8"
  }
];

let newArray = array_one.map(f => ({
  field2 : f.field1,
  field3 : f.field3,
  field6 : f.field6,
  field8 : f.field8,
}));

console.log(newArray);

3 Comments

hi @Weedoze ithink this is a typeScript syntax for Angular 2 , i'm using Angular js 1.X !
@Ali The code provided in the answer is totally not typescript and can either be used for Angular or AngularJS. This is pure ES6
you are right , it works fine thank you so much for your time

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.