1

I have an Index Signature with a number as a key and Person object as values. The order of the resulting array is not important.
How is it possible, to create an Array of all Person values from the Index Signature? At the end I am expecting an array containing the two Person objects.
Please see my simple example below and on jsfiddle.

Code:

interface Person {
  firstName: string;
  lastName:  string;
}

let firstUser = {
  firstName: "Malcolm",
  lastName:  "Reynolds"
};

let secondUser = {
  firstName: "Tom",
  lastName:  "Reynolds"
};

let indexSignature: {[key: number]: Person} = {};

indexSignature[158] = firstUser;
indexSignature[2] = secondUser;

// how to get an array of all Person objects in indexSignature?

jsfiddle:
Link

5
  • {key: number: Person} is not valid TypeScript syntax. An index signature should look like a computed property like {[key: number]: Person}. Commented May 7, 2020 at 15:44
  • Hmm, wow, and you haven't initialized indexSignature, just given it a type. So indexSignature[1] = ... is going to be a runtime error. And indices usually start with 0. Are you trying to start with 1 instead for some reason? If so you might want to mention it so it doesn't look like a typo. Backing up: I think this question could use a good minimal reproducible example where the only issue present is the one you are asking about. Commented May 7, 2020 at 15:48
  • I updated the code and the example to be runnable. The index of the numbers shouldn´t matter since the key can be a random, non zero-based integer Commented May 7, 2020 at 15:53
  • So you don't care about the order of the elements in the resulting array? Commented May 7, 2020 at 15:54
  • @jcalz no, the order of the elements doesn´t matter Commented May 7, 2020 at 15:57

1 Answer 1

3

This is a job for ES2017's Object.values(), which iterates over the properties of an object and returns an array of just the property values. The order will be the same as the order you get from Object.keys() or for..in, but you don't care about preserving the relative order of the entries anyway, so that's fine.

If you have access to Object.values() then you can simply write:

const arr = Object.values(indexSignature);
console.log(JSON.stringify(arr));
// [{"firstName":"Tom","lastName":"Reynolds"},
// {"firstName":"Malcolm","lastName":"Reynolds"}]

Looks good. If you don't have ES2017, then you'd need to polyfill it or rewrite it using Object.keys() or something:

const arr2 = Object.keys(indexSignature).map(k => indexSignature[+k]);
console.log(JSON.stringify(arr2));
// [{"firstName":"Tom","lastName":"Reynolds"},
// {"firstName":"Malcolm","lastName":"Reynolds"}]

Playground link to code

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

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.