admin管理员组

文章数量:1289509

I want to add an extra property to an array, but I get trouble with declaring it.

small part

const arr: string[] & {key: number} = ['123'];//ts error  Property 'key' is missing
arr.key = 10;

large part

I need to group arr by index then sort it by value

add an index to array property will be a convenience if using Object.values but not Object.entries. I know there are many approaches to do this, but I think it's much easier if I am using plain js

type Arr = {index: number, value: number};
const arr:Arr[] = []
arr.push({
    index: 10,
    value: 2
})
arr.push({
    index: 10,
    value: 1
})
arr.push({
    index: 10,
    value: 3
});
arr.push({
    index: 20,
    value: 100
});
arr.push({
    index: 20,
    value: 50
});
arr.reduce<Record<string, Arr[] & {index: number}>>((prev, curr) => {
    if(!prev[curr.index]){
        prev[curr.index] = [];//ts error Property missing
        prev[curr.index].index = curr.index;
    }
    prev[curr.index].push(curr);
    prev[curr.index].sort((a,b) => a.value - b.value);//or some insertion sort algorithm.
    return prev;
},{})

I want to add an extra property to an array, but I get trouble with declaring it.

small part

const arr: string[] & {key: number} = ['123'];//ts error  Property 'key' is missing
arr.key = 10;

large part

I need to group arr by index then sort it by value

add an index to array property will be a convenience if using Object.values but not Object.entries. I know there are many approaches to do this, but I think it's much easier if I am using plain js

type Arr = {index: number, value: number};
const arr:Arr[] = []
arr.push({
    index: 10,
    value: 2
})
arr.push({
    index: 10,
    value: 1
})
arr.push({
    index: 10,
    value: 3
});
arr.push({
    index: 20,
    value: 100
});
arr.push({
    index: 20,
    value: 50
});
arr.reduce<Record<string, Arr[] & {index: number}>>((prev, curr) => {
    if(!prev[curr.index]){
        prev[curr.index] = [];//ts error Property missing
        prev[curr.index].index = curr.index;
    }
    prev[curr.index].push(curr);
    prev[curr.index].sort((a,b) => a.value - b.value);//or some insertion sort algorithm.
    return prev;
},{})
Share Improve this question edited Aug 5, 2019 at 8:01 hem 1,0327 silver badges12 bronze badges asked Aug 5, 2019 at 7:17 dabusidedabuside 1131 silver badge8 bronze badges 3
  • how your are suppose to be ? [{index: number, value:string}] ? – Ariel Livshits Commented Aug 5, 2019 at 7:22
  • it shoule be { indexA: [{},{},{}], indexB:[{},{},{}], indexC:[{}], ... },each item in array is sorted bt its value – dabuside Commented Aug 5, 2019 at 7:57
  • possible duplicate of "Extending Array in TypeScript" – zhirzh Commented Aug 5, 2019 at 21:03
Add a ment  | 

2 Answers 2

Reset to default 10

An array literal can't by itself satisfy the intersection, the simplest way you could do it is to use Object.assign which returns an intersection of its arguments:

const arr: string[] & {key: number} = Object.assign(['123'], {
    key: 10
});

And in your bigger example, the same could be used:

arr.reduce<Record<string, Arr[] & {index: number}>>((prev, curr) => {
    if(!prev[curr.index]){
        prev[curr.index] = Object.assign([], {
          index: curr.index
        })
    }
    prev[curr.index].push(curr);
    prev[curr.index].sort((a,b) => a.value - b.value);//or some insertion sort algorithm.
    return prev;
},{})

playground link

Please consider the accepted answer instead.

In your small part example, the type declaration string[] & {key: number} can never be satisfied because an array of strings cannot be the same as an object with key property.

Concerning your large part example, what exactly are you trying to achieve? If you say you want to group an array by an index, and then sort it by value, do you mean that the index is kind of your primary sort criterion and value the secondary criterion? In that case, I'd suppose you write your own sorting function that takes both values into account. You'll find an example in this Stackoverflow thread.

本文标签: javascriptHow to add a extra property to array with typescriptStack Overflow