0

I have a code like below and it is working fine.

 for(let i=0;i< this.Array.length ; i++){
          if(this.Array[i].propertyObject.hasOwnProperty('header'))
          this.Array[i].ColumnName = this.Array[i].propertyObject.header;
    }

May i know how to achieve the same with Map. Thanks in advance.

4
  • 3
    This sounds like an X/Y problem. What makes you think you want to avoid a loop? Commented Feb 2, 2022 at 10:20
  • 1
    What about recursive functions? Commented Feb 2, 2022 at 10:22
  • @M.ÇağlarTUFAN - Ooooh, fair point!! :-) I mean, conceptually they're still loops, but they aren't technically loops. Commented Feb 2, 2022 at 10:23
  • 1
    :) to my eye that's a loop using the call stack, but 'formally' (as in for a sw interview) it can be the answer they're looking for Commented Feb 2, 2022 at 10:24

2 Answers 2

2

May i know how to achieve the same with Map

I assume you mean map. map isn't the right tool for doing exactly what that loop does, because that loop modifies the array in place, but map creates a new array instead.

If you want a new array, perhaps also with new objects (e.g., functional programming or immutable programming):

// Replace `this.Array` with a new array
this.Array = this.Array.map(element => {
    // If we need to change this element...
    if (element.propertyObject.hasOwnProperty("header")) {
        // ...do a shallow copy along with the replacement
        element = {...element, ColumnName: element.propertyObject.header};
    }
    return element;
});

Note that that assumes the elements are simple objects. If they aren't, you'll need to handle constructing the replacement differently than just using {...original}.

But if you want to keep the same array as your current code does, your loop is just fine. You have other options (like forEach or for-of), but what you have is also fine. for-of is well-suited to what you're doing:

for (const element of this.Array) {
    if (element.propertyObject.hasOwnProperty("header")) {
          element.ColumnName = element.propertyObject.header;
    }
}

Side note: In new code, you might want to use Object.hasOwn rather than Object.prototype.hasOwnProperty (with a polyfill if needed for older environments; recent versions of all modern browsers support it natively, though).

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

Comments

0
this.Array.forEach((item, index, arr)=> {
       item.ColumnName = "header" in item ? item.header : item.ColumnName;
});

5 Comments

Then ColumnName doesn't change.
Throwing the correction up to see if it sticks.
There is still a slight difference here (suppose ColumnName is an accessor property, not a data property), but it's very unlikely to matter. :-) (I mean, it would with innerHTML on a DOM element, but ColumnName seems unlikely to be an accessor with that kind of side-effect...)
Though arguably hasOwnProperty could be overwritten and return something completly different as well ;)
@pilchard - LOL Or header could be inherited, but the OP doesn't want that. But now we're getting absurd. :-D

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.