1

I'm still trying to navigate inside the map function but still need a little bit of help.

I'm trying to to gather all storeArea values in one individual array, and all totalStore.date values into different individual array. when I'm using map im getting for totalStore.date 6 arrays within an object. whats the right and correct thing to do? Thanks!

var results =[]

var stores_json = {
"stores": [
  {
    "storeArea": "area1",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area2",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area3",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area4",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area5",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  },
  {
    "storeArea": "area6",
    "totalStore": [
      {
        "date": "2018-10-01"
      },
      {
        "date": "2018-11-01"
      },
      {
        "date": "2018-12-01"
      }
    ]
  }
]
}

results = stores_json["stores"].map(function (x) {
    return [x.storeArea, x.totalStore.map(function (y) {
        var dates = [y.date]
        return dates
    })]
});
1
  • I edited the question. I need all date values in one single array and the same for storeArea. sorry for the mixup Commented Jan 17, 2019 at 7:57

1 Answer 1

1
areas = json.stores.map(function (x) {
    return x.storeArea
});
dates = json.stores.map(function (x) {
    return x.totalStore.map(function (y) {
        return y.date
    })
});
results = [areas,dates.flat()]

results is -

(2) [Array(6), Array(18)]
0: (6) ["area1", "area2", "area3", "area4", "area5", "area6"]
1: (18) ["2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01", "2018-10-01", "2018-11-01", "2018-12-01"]
Sign up to request clarification or add additional context in comments.

1 Comment

that's exactly what i needed. Thank you!

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.