When Extending Array map is undefined

Asked
Active3 hr before
Viewed126 times

8 Answers

array
90%

I expect to have Array2 (4) [10, 2, 5, 1] logged into the console but instead i get the following error thrown.,I guess you would like to reach more parameters into the constructor function. , Fast intercontinental Remote Desktop Sessions ,Edit Removing the constructor seems to fix the issue but does not explain why it fails when a constructor is present. especially when the constructor is just calling super.

Debugging with

constructor(items) {
   console.log(...arguments);
   super(...items);
}

Use

export default class Array2 extends Array {
   addOne() {
      return this.map((x) => {
         return x + 1;
      })
   }
}

const array2 = Array2.from([9, 1, 4, 0]).addOne();
const array3 = Array2.of(9, 1, 4, 0).addOne();
console.log(array2, array3);
load more v
88%

The issue I'm running into is that map is undefined. I can obviously work around this but I don't understand why as I am extending Array and therefore should have It's functionality?,Edit Removing the constructor seems to fix the issue but does not explain why it fails when a constructor is present. especially when the constructor is just calling super.,I expect to have Array2 (4) [10, 2, 5, 1] logged into the console but instead i get the following error thrown.,I guess you would like to reach more parameters into the constructor function.

The issue I'm running into is that map is undefined. I can obviously work around this but I don't understand why as I am extending Array and therefore should have It's functionality?

export default class Array2 extends Array {
   constructor(items) {
      super(...items);
   }

   addOne() {
      return this.map((x) => {
         return x + 1;
      })
   }
}

let arry2 = new Array2([9, 1, 4, 0]).addOne();
console.log(arry2);

I expect to have Array2 (4) [10, 2, 5, 1] logged into the console but instead i get the following error thrown.

Uncaught TypeError: undefined is not a
function
load more v
72%

var result = objArray.map(function(a) {
   return a.foo;
});
65%

While one might expect [1, 2, 3], the actual result is [1, NaN, NaN]. ,In this case, we return all the selected options' values on the screen:,When undefined or nothing is returned:,A new array with each element being the result of the callback function.

// Arrow function
map((element) => {
   ...
})
map((element, index) => {
   ...
})
map((element, index, array) => {
   ...
})

// Callback function
map(callbackFn)
map(callbackFn, thisArg)

// Inline callback function
map(function callbackFn(element) {
   ...
})
map(function callbackFn(element, index) {
   ...
})
map(function callbackFn(element, index, array) {
   ...
})
map(function callbackFn(element, index, array) {
   ...
}, thisArg)
load more v
75%

Array.from() is also a convenient alternative to using map() generically:,18.4.1. ECMAScript 6 treats holes like undefined elements, entries(), keys(), values() treat each hole as if it was the element undefined.,However, Array.from() treats holes as undefined:

const arrayLike = {
   length: 2,
   0: 'a',
   1: 'b'
};

// for-of only works with iterable values
for (const x of arrayLike) { // TypeError
   console.log(x);
}

const arr = Array.from(arrayLike);
for (const x of arr) { // OK, iterable
   console.log(x);
}
// Output:
// a
// b
load more v
40%

Multiply all the values in array with 10:,Return a new array with the square root of all the original values:,map() does not execute the function for empty elements.,Get the full name for each person in the array:

Definition and Usage

The map() method creates a new array with the results of calling a function for every array element.

map()
load more v
22%

array (Array): The array to compact.,array (Array): The array to convert.,array (Array): The array to modify.,array (Array): The array to fill.

_.chunk(['a', 'b', 'c', 'd'], 2); // => [['a', 'b'], ['c', 'd']] _.chunk(['a', 'b', 'c', 'd'], 3);// => [['a', 'b', 'c'], ['d']]
load more v
60%

Use for observable array-like objects.,Create an observable array-like object.,can.List is used to observe changes to an Array. can.List extends can.Map, so all the ways that you're used to working with Maps also work here.,the change event fires on every change to a List.

Use attr to read and write properties of a list:

var hobbies = new can.List(["JS", "Party Rocking"])
hobbies.attr(0) //-> "JS"
hobbies.attr("length") //-> 2

hobbies.attr(0, "JavaScript")

hobbies.attr() //-> ["JavaScript","Party Rocking"]
load more v

Other "array-undefined" queries related to "When Extending Array map is undefined"