-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.

3
  • Just do try coppyArray[i] == arr[i]. It returns copied value at index i Commented Aug 4, 2019 at 14:58
  • 3
    where is value coming from? Commented Aug 4, 2019 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). Commented Aug 4, 2019 at 15:00

2 Answers 2

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

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

1 Comment

(Okay, enough procrastinating, back to what I'm supposed to be doing now...)
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.

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.