-1

I have an array arr = [2,4]; I want to copy this array to another array coppyArray = [2,4]; . Now I have 2 conditions to fulfill.

  1. coppyArray[i].value() == arr[i]

  2. coppyArray.hasOwnProperty('value') equal to false;

I was using const coppyArray= [...arr]; but no luck. Can anybody please suggest how to achieve those condition using javascript or ES.

anirban karak
  • 732
  • 7
  • 20
  • Just do try `coppyArray[i] == arr[i]`. It returns copied `value` at index `i` – Orkhan Alikhanov Aug 04 '19 at 14:58
  • 3
    where is `value` coming from? – Nina Scholz Aug 04 '19 at 14:58
  • 1
    It's not clear what you're asking here. As @NinaScholz said, `value()`/`value` seems to come out of left field. Also, you've asked how to copy an array into another array, which your `const coppyArray = [...arr];` will do (it's one of several ways to do it, depending on what actual end result you want). – T.J. Crowder Aug 04 '19 at 15:00

2 Answers2

1

Now I have 2 conditions to fulfill.

  1. coppyArray[i].value() == arr[i]

  2. coppyArray.hasOwnProperty('value') equal to false;

The second condition is easy: arrays don't have a property called value, so we don't have to do anything special to satisfy that condition.

The only way to satisfy the first condition is to create an array of objects, for instance with map:

const arr = [2,4];
const copy = arr.map(n => ({value() { return n; }}));
console.log(copy[0].value() == arr[0]);    // true
console.log(copy.hasOwnProperty("value")); // false

If you've shared the second condition incorrectly and it was supposed to be that coppyArray[i].hasOwnProperty('value') is false, then we can use the prototype chain for the value function:

class Entry {
    constructor(n) {
        this.n = n;
    }
    value() {
        return this.n;
    }
}
const arr = [2,4];
const copy = arr.map(n => new Entry(n));
console.log(copy[0].value() == arr[0]);       // true
console.log(copy[0].hasOwnProperty("value")); // false

or without a constructor function:

const proto = {
    value() {
        return this.n;
    }
};
const arr = [2,4];
const copy = arr.map(n => {
    const entry = Object.create(proto);
    entry.n = n;
    return entry;
});
console.log(copy[0].value() == arr[0]);       // true
console.log(copy[0].hasOwnProperty("value")); // false
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Solution using JS Proxies:

const get = (target, prop, reciever) => prop === 'hasOwnProperty' ? () => false : ({ value: () => target[prop] });
const arr = [2,4];
const copyArr = arr => new Proxy(arr, {get});
const copyOfArr = copyArr(arr);

console.log(copyOfArr[0].value() == arr[0]) // true
console.log(copyOfArr[1].value() == arr[1]) // true
console.log(copyOfArr[1].value() == arr[0]) // false
console.log(copyOfArr[0].value() == arr[1]) // false
console.log(copyOfArr.hasOwnProperty("value")) // false

If you need to override the hasOwnProperty in a better way then read this.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99