1

i need to remove keys of the objects inside with another object using typescript.

let before = {
  '0': {
    'title':'title 1',
    'time':'12.30pm',
  },

  '1': {
    'title':'title 2',
    'time':'12.30pm',
  },

  '2': {
    'title':'title 3',
    'time':'12.30pm',
  },
}

expected results,

let after = [
  { 
    'title':'title 1',
    'time':'12.30pm',
  },

  {
    'title':'title 2',
    'time':'12.30pm',
  },

  {
    'title':'title 3',
    'time':'12.30pm',
  }
]
1
  • Have you tried using Object.values()? Basically just use that on the inner array and set the object to the returned result Commented Jul 2, 2019 at 18:00

2 Answers 2

6

Just use Object.values(before)

let before={0:{title:"title 1",time:"12.30pm"},1:{title:"title 2",time:"12.30pm"},2:{title:"title 3",time:"12.30pm"}};


console.log(Object.values(before));

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

Comments

1

To achieve expected result, use below option of using Object.entries and map

  1. Object entries returns array of object's key value pair
  2. Using map return value

let before = {
  '0': {
    'title':'title 1',
    'time':'12.30pm',
  },

  '1': {
    'title':'title 2',
    'time':'12.30pm',
  },

  '2': {
    'title':'title 3',
    'time':'12.30pm',
  },
}

console.log(Object.entries(before).map(v => v[1]))

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.