1

I have some problem in creating the required form of an array. I want to delete the key named service_id and put their data into the outside.

 "restaurant_services": Array[2][
            {
              "service_id": {
                "added_on": "2018-07-07T07:56:35.054Z",
                "deleted": false,
                "_id": "5b4072332c94ca607ffcf65b",
                "service_name": "Cafe",
                "service_status": 1,
                "__v": 0
              },
              "_id": "5b433cd7e066e6623b9c9ce6"
            },
            {
              "service_id": {
                "added_on": "2018-07-07T10:01:05.083Z",
                "deleted": false,
                "_id": "5b408f61caeca976851923c4",
                "service_name": "Dine In",
                "service_status": 1,
                "__v": 0
              },
              "_id": "5b433cd7e066e6623b9c9ce5"
            }
    ],

i would like to convert the above array to below.

"restaurant_services": Array[2][
        {
            "added_on": "2018-07-07T07:56:35.054Z",
            "deleted": false,
            "_id": "5b4072332c94ca607ffcf65b",
            "service_name": "Cafe",
            "service_status": 1,
            "__v": 0
        },
        {
            "added_on": "2018-07-07T10:01:05.083Z",
            "deleted": false,
            "_id": "5b408f61caeca976851923c4",
            "service_name": "Dine In",
            "service_status": 1,
            "__v": 0
        }
],

Any Suggestion For that to covert this type of array?

2
  • 6
    you can use arr.map(({service_id})=>service_id) Commented Jul 10, 2018 at 7:23
  • It looks like your are using MongoDB. Are you looking for the $replaceRoot operator? Commented Jul 10, 2018 at 7:34

2 Answers 2

1

You can use map to remove the service_id. Here is the snippet below.

Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

const restaurant_services = [
  {
    "service_id": {
      "added_on": "2018-07-07T07:56:35.054Z",
      "deleted": false,
      "_id": "5b4072332c94ca607ffcf65b",
      "service_name": "Cafe",
      "service_status": 1,
      "__v": 0
    },
    "_id": "5b433cd7e066e6623b9c9ce6"
  },
  {
    "service_id": {
      "added_on": "2018-07-07T10:01:05.083Z",
      "deleted": false,
      "_id": "5b408f61caeca976851923c4",
      "service_name": "Dine In",
      "service_status": 1,
      "__v": 0
    },
    "_id": "5b433cd7e066e6623b9c9ce5"
  }
]

const restaurant_services_updated = restaurant_services.map(service => service.service_id);

console.log(restaurant_services_updated)

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

Comments

0

Use Array.prototype.map() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

In your case

var originalArr  = [
        {
          "service_id": {
            "added_on": "2018-07-07T07:56:35.054Z",
            "deleted": false,
            "_id": "5b4072332c94ca607ffcf65b",
            "service_name": "Cafe",
            "service_status": 1,
            "__v": 0
          },
          "_id": "5b433cd7e066e6623b9c9ce6"
        },
        {
          "service_id": {
            "added_on": "2018-07-07T10:01:05.083Z",
            "deleted": false,
            "_id": "5b408f61caeca976851923c4",
            "service_name": "Dine In",
            "service_status": 1,
            "__v": 0
          },
          "_id": "5b433cd7e066e6623b9c9ce5"
        }
]

var  newArr = arr.map(function(currValue){


 return currValue.service_id
 })

console.log('desired result',newArr)

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.