Can't use array functions like .includes in Object literals

Asked
Active3 hr before
Viewed126 times

10 Answers

arrayfunctionsincludesliteralsobject
90%

The original shape of the state was this:,But then you changed it:,Use separate states for filter and other.

{
   filter: [],
   other: []
}
load more v
88%

let list1 = [{
      name: "object1"
   },
   {
      name: "object2"
   },
   {
      name: "object3"
   },
   {
      name: "object4"
   }
]

var contains = list1.some(elem => {
   return JSON.stringify({
      name: "object1"
   }) === JSON.stringify(elem);
});
if (contains) {
   document.write('contains')
} else {
   document.write('doesnt')
}
load more v
72%

The elements in an array are stored as the array’s properties, using numbers as property names. Because you can’t use the dot notation with numbers and usually want to use a binding that holds the index anyway, you have to use the bracket notation to get at them.,There are some named properties in arrays, such as length and a number of methods. Methods are functions that live in properties and (usually) act on the value they are a property of.,Square bracket array notation similarly allows the triple-dot operator to spread another array into the new array.

To run over a list (in listToArray and nth), a for loop specification like this can be used:

for (let node = list; node; node = node.rest) {}
65%

Alternatively, you can create an object with these two steps:,Transitioning to strict mode,Create an instance of the object with new.

objectName.propertyName
load more v
75%

For array literals or strings:,Expressions and operators,Warning: expression closures are deprecated

function sum(x, y, z, n) {
   return x + y + z + n;
}
load more v
40%

Using {} instead of new Object(); is know as “Object Literal” syntax.,Did you know you can send any number of arguments you want to a javascript function? They're all stored in an array-like object named arguments.,Second, arrays are objects, so you can do this, but it can get you some serious inconsistencies:

A javascript object is a basic data structure:

var basicObj = {}; // an empty object - {} is a shortcut for "new Object()"

basicObj.suprise = "cake!";

basicObj['suprise']; // returns "cake!"
load more v
22%

separate properties with comma,To access the values on this object, you can use “dot notation”, that is, the name of the object, a dot, and the name of the property.,Where did toUpperCase come from? Here you have used an Object. Every time you create a String variable, you’re actually creating a String Object. This object has properties and methods, toUpperCase being just one of them. More details about the String object here. Get used to the word “Object”, because is the foundation of modern programming.

Perhaps you don’t know them by name, but the truth is that when you work with Javascript, you’re working with Objects all the time without realizing it. Take a look at the following example:

var carmake = 'Honda';
console.log(carmake.toUpperCase());
  • Enclose it in curly brackets
  • separate properties with comma
  • separate keys and values with a colon
var person = {
   name: 'jack',
   email: 'jack@ctu.com',
   twitter: '@jackb_ctu'
};

To access the values on this object, you can use “dot notation”, that is, the name of the object, a dot, and the name of the property.

console.log(person.name);
console.log(person.twitter);
  • Enclose it in square brackets
  • separate each element with comma
var arr = [
   'value1',
   'value2',
   'value3'
];

To access the values, you use a numerical index:

console.log(arr[0]);

var arr = [{
      key1: 'value1'
   },
   {
      key2: 'value2'
   }
];
console.log(arr[0].key1);

var obj = {
   key: [
      'value1',
      'value2'
   ]
};
console.log(obj.key[1]);

var arr = [{
   key: [
      'value1',
      'value2'
   ]
}];
console.log(arr[0].key[1]);

Titanium itself is a JavaScript SDK (Software Development Kit) that works as an “Object Factory”. This means that it has methods that generate Objects, and most of the times these methods receive Objects as arguments. It sounds more confusing than it is.

var win = Titanium.UI.createWindow({
   backgroundColor: '#fff',
   fullscreen: true
})
load more v
60%

We use Objects all the time, either as constructors or literals. Often, we use them for Object lookup purposes, to get values from Object properties.,The difference is we need to call the Object literal’s function:,Let’s set up a simple Object literal that returns a String value only.

var type = 'coke';
var drink;
switch (type) {
   case 'coke':
      drink = 'Coke';
      break;
   case 'pepsi':
      drink = 'Pepsi';
      break;
   default:
      drink = 'Unknown drink!';
}
console.log(drink); // 'Coke'
load more v
48%

The good part is that ES2015 allows to use the literal __proto__ as the property name to setup the prototype right in the object literal { __proto__: protoObject }.,Let’s use __proto__ property for an object initialization, and improve the bitter situation that was described in the introduction:,You can setup the object’s prototype directly from the initiliazer using __proto__ property name. Which is easier than dealing with Object.create().

var myObject = {
   myString: 'value 1',
   get myNumber() {
      return this._myNumber;
   },
   set myNumber(value) {
      this._myNumber = Number(value);
   },
};
myObject.myString; // => 'value 1'
myObject.myNumber = '15';
myObject.myNumber; // => 15
load more v
23%

 Pretag team - issue, fix, solve, resolve

Other "array-functions" queries related to "Can't use array functions like .includes in Object literals"