2

How can i get only the Date without the string value from below object?

0: {2020-09-02: "string_1", 2020-09-03: "string_2"}
1: {2020-09-01: "string_1", 2020-09-05: "string_2"}

My objective is to get the Date only and group them into one array.

Expected result: [2020-09-02, 2020-09-03, 2020-09-01, 2020-09-05]

What I tried so far is using Object.getOwnPropertyNames:

console.log('property name: ', Object.getOwnPropertyNames(getDateProperties)) // return ["0","1"]

Is this something possible to achieve?

5
  • Have you tried with Object.keys(getDateProperties) Commented Aug 16, 2020 at 11:53
  • yes did tried it before but result is similar which return, ["0", "1"] @Asutosh Commented Aug 16, 2020 at 11:54
  • are you sure your data is an Object and not an Array of objects? Commented Aug 16, 2020 at 11:56
  • You need to access inner object keys Commented Aug 16, 2020 at 11:56
  • @1aliff I have posted the answer using Object.keys. Please have a look. Commented Aug 16, 2020 at 11:59

5 Answers 5

1

Use reduce and Object keys to get the dates from the object

const list = [{
    "2020-09-02": "string_1",
    "2020-09-03": "string_2"
  },
  {
    "2020-09-01": "string_1",
    "2020-09-05": "string_2"
  }
]
const result = list.reduce((acc, x) => {
  const keys = Object.keys(x)
  acc = [...acc, ...keys]
  return acc;
}, [])
console.log(result)

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

Comments

1

Try this:

let obj = {
0: {"2020-09-02": "string_1", "2020-09-03": "string_2"},
1: {"2020-09-01": "string_1", "2020-09-05": "string_2"}
}
let arr = [];
for(var key in obj){
    for(var subkey in obj[key]){
        arr.push(subkey);
    }
}
console.log('property name: ', arr)

Comments

1

You may try like this:

let obj = {
  0: {
    "2020-09-02": "string_1",
    "2020-09-03": "string_2"
  },
  1: {
    "2020-09-01": "string_1",
    "2020-09-05": "string_2"
  }
}
const resultArray = [];
Object.keys(obj).forEach((k) => Object.keys(obj[k]).forEach((dateArg) => resultArray.push(dateArg)));
console.log(resultArray);

Comments

0

Quick one-liner

let obj = {
    0: {"2020-09-02": "string_1", "2020-09-03": "string_2"},
    1: {"2020-09-01": "string_1", "2020-09-05": "string_2"}
}

let output = Object.keys(obj).reduce((acc, key) => [...acc, ...Object.keys(obj[key])], [])

console.log(output)

Comments

0

Use flatMap and Object.keys should simplify

const list = [
  {
    "2020-09-02": "string_1",
    "2020-09-03": "string_2",
  },
  {
    "2020-09-01": "string_1",
    "2020-09-05": "string_2",
  },
];

const res = list.flatMap(Object.keys);

console.log(res);

Alternatively, if the input data is object.

let obj = {
  0: {
    "2020-09-02": "string_1",
    "2020-09-03": "string_2"
  },
  1: {
    "2020-09-01": "string_1",
    "2020-09-05": "string_2"
  }
}

const res = Object.values(obj).flatMap(Object.keys);

console.log(res)

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.