Now I have 2 conditions to fulfill.
coppyArray[i].value() == arr[i]
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
coppyArray[i] == arr[i]. It returns copiedvalueat indexivaluecoming from?value()/valueseems to come out of left field. Also, you've asked how to copy an array into another array, which yourconst coppyArray = [...arr];will do (it's one of several ways to do it, depending on what actual end result you want).