1

I have a json array like below, I need to get all the values from below array and need to form a new array of array objects

But I cannot use any model files to achieve this. Not sure how to do this in typescript.

Please help me on this.

[
  {
    "firstName": "Rajesh",
    "lastName": "Kumar",
    "age": "25"
  },
  {
    "firstName": "john",
    "lastName": "david",
    "age": "26"
  }
]

I need to output like

[["Rajesh","Kumar","25"],["john","david","26"]]
1
  • 1
    There is nothing to do with Typescript here. You could do it with JS Array#map + Object.values(). Try: inputArray.map(obj => Object.values(obj)). Commented Jul 14, 2021 at 11:02

4 Answers 4

3

So, what you want to do is to map all the values in the array using <Array>.map. Then you can return the object as an Array in 2 ways, first is using <Object>.values or by just doing [obj.firstName, obj.lastName, obj.age]

You could try Looping through the array and change the values as well using a for loop, but that it really verbose. I'd recommend the 1st approach, since that's more dynamic in this case.

So the resulting code will be

let data = [
  {
    "firstName": "Rajesh",
    "lastName": "Kumar",
    "age": "25"
  },
  {
    "firstName": "john",
    "lastName": "david",
    "age": "26"
  }
];
// I'm assigning the value to the variable called data in this case

data = data.map(item => Object.values(item));

// Now the value of data is the array you want it to be

Now you can follow the other approaches as I specified, however this is the least verbose way to go around.

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

Comments

0

This will give you what you are looking for

const array = [
  {
    "firstName": "Rajesh",
    "lastName": "Kumar",
    "age": "25"
  },
  {
    "firstName": "john",
    "lastName": "david",
    "age": "26"
  }
];

// Under result you will have the required array
const result = a.map(x => [Object.keys(x).map(key => x[key])])

// Or, explicit
const result = array.map(x => [x.firstName,x.lastName,x.age])

Comments

0

Since you have limited elements inside each object you could do:

const collections = [
  {
    firstName: 'Rajesh',
    lastName: 'Kumar',
    age: '25'
  },
  {
    firstName: 'john',
    lastName: 'david',
    age: '26'
  }
]

const answer = collections.map(c => [c.firstName, c.lastName, c.age])
console.log(answer)

Comments

0

You can try this:

let newArrObj: any[] = []; 

// objects - your array of objects

for (let obj in objects) {

  let objArray= []; 
  objArray.push(obj.firstName)
  objArray.push(obj.lastName)
  objArray.push(obj.age)
  
  newArrObj.push(objArray);
 }
 
 console.log("Result newArrObj: ", newArrObj);

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.