Skip to content
Advertisement

How to put index as recognizing same number of sorted array in JavaScript?

I’m dealing with immutable objects.

I sort data by thisweekNumber as you can see below.

 data.sort(function (a, b) {
            return b.thisweekNumber - a.thisweekNumber;
          })

For exmaple,

data = [
{name: A, thisweekNumber: 3}, 
{name: B, thisweekNumber: 5}, 
{name: C, thisweeknumber: 1}, 
{name: D, thisweekNumber: 4}
{name: E, thisweekNumber: 4}
]

I sort them B -> D -> E -> A -> C

So I want to put Index to them in this order:

data = [
{name: A, thisweekNumber: 3, index: 3}, 
{name: B, thisweekNumber: 5, index: 1}, 
{name: C, thisweeknumber: 1, index: 4}, 
{name: D, thisweekNumber: 4, index: 2}
{name: E, thisweekNumber: 4, index: 2}
]

BUT here, I want to recognize same thisweekNumber of D and E.

Since they both have 4.

So Index will be like above.

How can I make this array in JavaScript?

Advertisement

Answer

The simplest thing I can think of is to do it after sorting, with a simple loop:

let index = 1;
for (let n = 0; n < data.length; ++n) {
    const current = data[n];
    current.index = index;
    // See if the next one (if any) matches this one
    if (data[n + 1]?.thisweekNumber !== current.thisweekNumber) {
        ++index;
    }
}

const data = [
    { name: "A", thisweekNumber: 3 },
    { name: "B", thisweekNumber: 5 },
    { name: "C", thisweeknumber: 1 },
    { name: "D", thisweekNumber: 4 },
    { name: "E", thisweekNumber: 4 },
];
data.sort(function (a, b) {
    return b.thisweekNumber - a.thisweekNumber;
    // return b.thisweekFeedNumber - a.thisweekFeedNumber;
});

let index = 1;
for (let n = 0; n < data.length; ++n) {
    const current = data[n];
    current.index = index;
    // See if the next one (if any) matches this one
    if (data[n + 1]?.thisweekNumber !== current.thisweekNumber) {
        ++index;
    }
}
console.log(data);
.as-console-wrapper {
    max-height: 100% !important;
}

Note the use of optional chaining in the if.


In a comment you’ve said:

thanks i understand the concept ! but my object seems not extensible in the process ‘current.index = index’. erorr says : cannot add property index, object is not extensible.

You didn’t mention that in the question. If you’re dealing with immutable objects, you’ll need to create new objects (probably in a new array). Creating the objects is typically done using spread syntax:

const newObject = {
    ...oldObject,
    newProperty: "new value"
};

You can easily do that in the loop above, pushing to an in-scope array:

const data = [
    { name: "A", thisweekNumber: 3 },
    { name: "B", thisweekNumber: 5 },
    { name: "C", thisweeknumber: 1 },
    { name: "D", thisweekNumber: 4 },
    { name: "E", thisweekNumber: 4 },
];
data.sort(function (a, b) {
    return b.thisweekNumber - a.thisweekNumber;
    // return b.thisweekFeedNumber - a.thisweekFeedNumber;
});

let index = 1;
const result = [];
for (let n = 0; n < data.length; ++n) {
    const current = data[n];
    result.push({
        ...current,
        index,
    });
    // See if the next one (if any) matches this one
    if (data[n + 1]?.thisweekNumber !== current.thisweekNumber) {
        ++index;
    }
}
console.log(result);
.as-console-wrapper {
    max-height: 100% !important;
}

…or via map:

const data = [
    { name: "A", thisweekNumber: 3 },
    { name: "B", thisweekNumber: 5 },
    { name: "C", thisweeknumber: 1 },
    { name: "D", thisweekNumber: 4 },
    { name: "E", thisweekNumber: 4 },
];
data.sort(function (a, b) {
    return b.thisweekNumber - a.thisweekNumber;
    // return b.thisweekFeedNumber - a.thisweekFeedNumber;
});

let index = 1;
const result = data.map((current, n) => {
    const element = {
        ...current,
        index,
    };
    // See if the next one (if any) matches this one
    if (data[n + 1]?.thisweekNumber !== current.thisweekNumber) {
        ++index;
    }
    return element;
});
console.log(result);
.as-console-wrapper {
    max-height: 100% !important;
}

The map version is probably more idiomatic.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement