Lazy Loading Image through attribute ( #shorts )
Hi Friends,
Lazy loading is a technique we can use in our web development to defer the loading of non-critical resources like images, scripts, or stylesheets until they are actually needed.
we can use the loading attribute on the img element and loading attribute can take three values:
auto: The browser decides when to load the image (this is browsers default behavior).
lazy: The browser can defer loading the image until it's within the user's viewport.
eager: The browser should load the image immediately.
example(replace example with actual image tags):
// Normal Image Loading
[img src="normal-image.jpg" alt="Normal Image"]
// Lazy Loading Image
[img src="placeholder-image.jpg"
data-src="lazy-image.jpg" alt="Lazy Loaded Image"
loading="lazy"]
#javascript #image #imagemetatags #metatags #lazy #loading #lazyloading #random #unique #html #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
5
views
`_.property` function on object using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.property` function on object using JavaScript Library underscore.js
`_.property` function in Underscore.js library creates a function that returns the value of a specified property on an object. It is a easy way to get a specific property value from an object.
In below example, we create a function getName using _.property('name'). This function is now bound to the 'name' property. We then have an object person with properties like 'name' and 'age'. When we call getName(person), it retrieves the value of the 'name' property from the person object, which is "Annie".
Note: _.property function returns a new function that, when called with an object as its argument, retrieves the value of the specified property on that object.
// syntax
_.property(key)
// example
var getName = _.property('name');
var person = { name: 'Annie', age: 25 };
console.log(getName(person));
// "Annie"
#javascript #under #score #underscore #underscorejs #property #hastag #underscoreproperty #propertyfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
3
views
`_.has` function on object using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.has` function on object using JavaScript Library underscore.js
`_.has` is a utility function in Underscore.js library that allows us to check if an object contains/has a specific property regardless of the property's value.
In below example, we have an object person that has a person's details. We use _.has to check if the person object has the properties "name" and "phone". The first call to _.has returns true because the person object has the "name" property. The second call returns false because the person object does not have the "phone" property.
Note: _.has can be useful when we want to conditionally access or manipulate a property only if it exists in the object.
// syntax
_.has(object, key)
// example
var person = { name: "Annie", age: 25, city: "New York" };
console.log(_.has(person, "name"));
// true
console.log(_.has(person, "phone"));
// false
#javascript #under #score #underscore #underscorejs #has #hastag #underscoreget #getfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
#2 `_.get` function on object using JavaScript Library underscore.js ( #shorts )
Hi Friends,
method #2 `_.get` function on object using JavaScript Library underscore.js
`_.get` is a utility function in Underscore.js library is commonly used to retrieve the value of a nested property within an object, even if some intermediate properties do not exist without facing errors due to missing or undefined intermediate properties.
In below example, we have an object person with nested properties representing a person's details. We use _.get to retrieve the number property within the nested street object. The result is 123.
Note: _.get helps you handle situations where intermediate properties may be missing or undefined, preventing errors and allowing you to provide fallback values if needed.
// syntax
_.get(object, path, [defaultValue])
// example
var person = { name: "Annie", address: { city: "New York",
street: { number: 123, name: "Main Street" }
}
};
var streetNumber = _.get(person, ["address", "street", "number"]);
console.log(streetNumber);
// 123
#javascript #under #score #underscore #underscorejs #get #underscoreget #getfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.get` function on object using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.get` function on object using JavaScript Library underscore.js
`_.get` is a utility function in Underscore.js library is commonly used to retrieve the value of a nested property within an object, even if some intermediate properties do not exist without facing errors due to missing or undefined intermediate properties.
In below example, we have an object person with nested properties representing a person's details. We use _.get to retrieve the value of the city property within the address object. The result is "New York".
Note: _.get helps you handle situations where intermediate properties may be missing or undefined, preventing errors and allowing you to provide fallback values if needed.
// syntax
_.get(object, path, [defaultValue])
// example
var person = { name: "Annie", address: {
city: "New York",
street: { number: 123, name: "Main Street" }
}
};
var cityName = _.get(person, "address.city");
console.log(cityName);
// "New York"
#javascript #under #score #underscore #underscorejs #get #underscoreget #getfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.clone` function on object or array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.clone` function on object or array using JavaScript Library underscore.js
`_.clone` is a utility function in Underscore.js library is commonly used when you want to duplicate an object or array without modifying the original one.
In below example, we have an array originalArray with some elements. We use _.clone to create a shallow copy of the array and assign it to clonedArray. Then, we modify clonedArray by pushing an additional element. As a result, the originalArray remains unchanged, while the clonedArray contains the additional element.
Note: Changes made to the cloned object or array will not affect the original one, and vice versa.
// syntax
_.clone(value)
// example
var originalArray = [1, 2, 3, 4, 5];
var clonedArray = _.clone(originalArray);
clonedArray.push(6);
console.log(originalArray);
// [1, 2, 3, 4, 5]
console.log(clonedArray);
// [1, 2, 3, 4, 5, 6]
#javascript #under #score #underscore #underscorejs #clone #underscoreclone #clonefunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
11
views
`_.defaults` function on object using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.defaults` function on object using JavaScript Library underscore.js
`_.defaults` is a utility function in Underscore.js library that ensure if an object has certain properties with default values, in case those properties are missing or undefined.
In below example, we have an object options that represents various options. We use _.defaults to set default values for the city and profession properties in case they are missing or undefined in the options object. After applying _.defaults, the options object is modified to include the default values for those properties.
Note: You can pass multiple default objects as arguments to _.defaults.
// syntax
_.defaults(object, *defaults)
// example
var options = {
name: "Alice",
age: 26
};
_.defaults(options, {
city: "New York",
profession: "Engineer"
});
console.log(options);
// { name: "Alice", age: 26, city: "New York", profession: "Engineer" }
#javascript #under #score #underscore #underscorejs #default #underscoredefaults #defaultsfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.omit` function on object using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.omit` function on object using JavaScript Library underscore.js
`_.pick` is a utility function in Underscore.js library that allows you to create a new object by exclude certain properties from an object without those properties.
In below example, we have an object person that represents a person's details. We use _.omit to exclude the age and profession properties from the person object. The variable omittedProps is a new object that contains all properties from the person object except age and profession.
Note: You can pass multiple property keys as arguments to _.omit, and it will create a new object without those properties if they exist in the source object.
// syntax
_.omit(object, *keys)
// example
var person = { name: "Alice", age: 25,
city: "New York", profession: "Engineer"};
var omittedProps = _.omit(person, "age", "city");
console.log(omittedProps);
// { name: "Alice", profession: "Engineer"}
#javascript #under #score #underscore #underscorejs #omit #underscoreomit #omitfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.where` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.where` function on array using JavaScript Library underscore.js
`_.where` is a utility function in Underscore.js library that allows you to filter a collection based on multiple key-value pairs. It returns an array of all objects from the collection that match the specified properties.
In below example, we have an array of user objects users. We use _.where to filter the collection and retrieve all objects that have the property city set to "London". The result, filteredUsers, is an array containing the user objects that match the specified key-value pair.
Note: _.where returns an array containing all objects from the collection that match the specified key-value pairs.
// syntax
_.where(list, properties)
// example
var users = [
{ name: "Alice", age: 25, city: "New York" },
{ name: "Bob", age: 30, city: "London" },
{ name: "Charlie", age: 20, city: "Paris" },
{ name: "David", age: 35, city: "London" }
];
var filteredUsers = _.where(users, { city: "London" });
console.log(filteredUsers);
// [{ name: "Bob", age: 30, city: "London" },
// { name: "David", age: 35, city: "London" }]
#javascript #under #score #underscore #underscorejs #findWhere #findwhere #find #where #underscorefindwhere #findwherefunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
6
views
`_.findWhere` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.findWhere` function on array using JavaScript Library underscore.js
`_.findWhere` is a utility function in Underscore.js library that allows you to find the first object in a collection that matches a set of key-value pairs.
In below example, we have an array of user objects users. We use _.findWhere to search for the first user object that has the property city set to "London". The result, foundUser, is the first user object found that matches the specified key-value pair.
Note: _.findWhere returns the first object in the collection that matches the specified key-value pairs. If no match is found, it returns undefined.
// syntax
_.findWhere(list, properties)
// example
var users = [
{ name: "Alice", age: 25, city: "New York" },
{ name: "Bob", age: 30, city: "London" },
{ name: "Charlie", age: 20, city: "Paris" }
];
var foundUser = _.findWhere(users, { city: "London" });
console.log(foundUser);
// { name: "Bob", age: 30, city: "London" }
#javascript #under #score #underscore #underscorejs #findWhere #findwhere #find #where #underscorefindwhere #findwherefunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.find` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.find` function on array using JavaScript Library underscore.js
`_.find` is a utility function in Underscore.js library that allows you to find the first element in a collection that satisfies a given condition. It provides a simple way to search for an element based on a specified criterion.
In below example, we have an array of numbers numbers. We use _.find to search for the first even number in the array. The provided predicate function checks if the number is divisible by 2. The result, foundNumber, is the first even number found, which is 2.
Note: _.find can also be used with objects
// syntax
_.find(collection, predicate, [context])
// example
var numbers = [1, 2, 3, 4, 5];
var foundNumber = _.find(numbers, function(number) {
return number % 2 === 0; // Find the first even number
});
console.log(foundNumber);
// 2
#javascript #under #score #underscore #underscorejs #find #underscorefind #findfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.map` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.map` function on array using JavaScript Library underscore.js
`_.map` is a utility function in Underscore.js library that allows you to transform each element in a collection using a specified transformation function. It provides a way to apply a common operation or transformation to each element and collect the results in a new array.
In below example, we have an array of numbers numbers. We use _.map to apply the transformation function, which multiplies each number by 2, to each element in the array. The resulting doubledNumbers array contains the transformed values [2, 4, 6, 8, 10].
Note: _.map can also be used with objects
// syntax
_.map(collection, iteratee)
// example
var numbers = [1, 2, 3, 4, 5];
var doubledNumbers = _.map(numbers, function(number) {
return number * 2;
});
console.log(doubledNumbers);
// [2, 4, 6, 8, 10]
#javascript #under #score #underscore #underscorejs #map #underscoremap #mapfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.each` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.each` function on array using JavaScript Library underscore.js
`_.each` is a utility function in Underscore.js library that allows you to iterate over elements in a collection (an array or an object). It provides a way to perform a specified operation or execute a callback function for each element in the collection.
In below example, we have an array of numbers numbers. We use _.each to iterate over each element in the array. For each element, the provided anonymous function is executed, which simply logs the number to the console. As a result, each number in the array will be printed to the console.
Note: _.each can also be used with objects
// syntax
_.each(collection, iteratee)
// example
var numbers = [1, 2, 3, 4, 5];
_.each(numbers, function(number) {
console.log(number);
});
// 1, 2, 3, 4, 5
#javascript #under #score #underscore #underscorejs #each #underscoreeach #eachfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.sample` function on array using JavaScript Library underscore.js
Hi Friends,
`_.sample` function on array using JavaScript Library underscore.js
`_.sample` is a utility function in Underscore.js library that allows you to retrieve a random element from an array. It provides a simple way to select a random item from a collection.
In below example, we have an array of numbers numbers. We use _.sample to retrieve a random element from the array and assign it to the variable randomNum. Each time you run the code, you will get a different random number from the array. We can also use _.sample with objects. In that case, it will return a random value from the object.
// syntax
_.sample(collection)
//example
var numbers = [1, 2, 3, 4, 5];
var randomNum = _.sample(numbers);
console.log(randomNum);
// Example output: 3 (random number selected from the array)
var obj = { name: "Alice", age: 30, city: "New York" };
var randomValue = _.sample(obj);
console.log(randomValue);
// Example output: "New York" (random selected value from the object)
#javascript #under #score #underscore #underscorejs #sample #underscoresample #samplefunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.shuffle` function on array using JavaScript Library underscore.js
Hi Friends,
`_.shuffle` function on array using JavaScript Library underscore.js
`_.shuffle` is a utility function in Underscore.js library that allows you to randomly shuffle the elements of an array. It provides a simple way to randomize the order of elements in a collection.
In below example, we have an array of numbers numbers. We use _.shuffle to create a new array shuffledNumbers with the same elements as numbers but in a random order. The output will vary each time you run the code due to the random nature of the shuffling.
Note: _.shuffle does not modify the original array; it returns a new shuffled array. If you want to shuffle the elements in place, you can assign the result back to the original array.
// syntax
_.shuffle(array)
// example
var numbers = [1, 2, 3, 4, 5];
var shuffledNumbers = _.shuffle(numbers);
console.log(shuffledNumbers);
// Example output: [3, 1, 5, 4, 2]
#javascript #under #score #underscore #underscorejs #shuffle #underscoreshuffle #shufflefunction #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
2
views
`_.pluck` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.pluck` function on array using JavaScript Library underscore.js
`_.pluck` is a utility function in Underscore.js library that allows you to extract a specific property value from each object in an array of objects. It simplifies the process of retrieving a particular property from multiple objects.
In below example, we have an array of objects students, where each object represents a student with properties such as name and age. We use _.pluck to extract the name property from each object, and the resulting names array contains the names of the students.
Note: starting from Underscore.js version 1.8.0, the _.pluck function has been deprecated in favor of the _.map function with a property shorthand.
// syntax
_.pluck(list, propertyName)
// example
var students = [
{ name: "Alice", age: 20 },
{ name: "Bobby", age: 22 },
{ name: "Charlie", age: 21 }
];
var names = _.pluck(students, "name");
console.log(names);
// ["Alice", "Bobby", "Charlie"]
#javascript #under #score #underscore #underscorejs #underscorepluck #pluckfunction #pluck #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #best #free #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official ​
7
views
`_.range` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.range` function on array using JavaScript Library underscore.js
`_.range` is a utility function in Underscore.js library that allows you to generate a sequence of numbers in a specified range.
In below example, we use _.range to generate a sequence of numbers starting from 1, up to, but not including 10, with a step of 2. The resulting numbers array contains the numbers [1, 3, 5, 7, 9].
Note: _.range returns a new array containing the generated sequence of numbers from the start value up to, but not including, the stop value, incrementing by the step value.
// syntax
_.range([start], stop, [step])
//example
var numbers = _.range(1, 10, 2);
console.log(numbers);
// [1, 3, 5, 7, 9]
var numbers = _.range(5, 0, -1);
console.log(numbers);
// [5, 4, 3, 2, 1]
var numbers = _.range(5);
console.log(numbers);
// [0, 1, 2, 3, 4]
#javascript #under #score #underscore #underscorejs #underscorerange #rangefunction #range #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #best #free #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
8
views
`_.unzip` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.unzip` function on array using JavaScript Library underscore.js
`_.unzip` is a utility function in Underscore.js library that allows you to convert an array of tuples into multiple arrays, each containing elements from a single position in the list.
In below example, we start with an array of lists people that we created using `_.zip`. We use `_.unzip` to reverse the operation and convert the lists back into separate arrays. The resulting `unzipped` array contains three arrays, one for each original input array.
// syntax
_.unzip(array)
// example
var people = [
["Angel", 30, 68],
["Bob", 25, 72],
["Cherry", 35, 70]
];
var unzipped = _.unzip(people);
console.log(unzipped);
// [
// ["Angel", "Bob", "Cherry"],
// [30, 25, 35],
// [68, 72, 70]
// ]
#javascript #under #score #underscore #underscorejs #unzip #underscoreunzip #unzipfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.zip` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.zip` function on array using JavaScript Library underscore.js
`_.zip` is a utility function in Underscore.js library that allows you to combine multiple arrays into a single array of list. Each list contains one element from each of the input arrays, aligned by their positions.
In below example, we have three arrays: `names`, `ages`, and `heights`. We use `_.zip` to combine them into a single array `people` of list, where each list contains one element from each of the input arrays. The resulting `people` array contains lists for each person, with their name, age, and height in inches.
// syntax
_.zip(*arrays)
// example
var names = ["Angel", "Bob", "Charry"];
var ages = [30, 25, 35];
var heights = [68, 72, 70];
var people = _.zip(names, ages, heights);
console.log(people);
// [
// ["Angel", 30, 68],
// ["Bob", 25, 72],
// ["Charry", 35, 70]
// ]
#javascript #under #score #underscore #underscorejs #zip #underscorezip #zipfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.uniq` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.uniq` function on array using JavaScript Library underscore.js
`_.uniq` is also utility function in Underscore.js library that allows you to create a new array that contains only the unique elements from an existing array.
In below example, we have an array `people` that contains duplicate objects with different ages. We use `_.uniq` with an iteratee function that returns the person's name, to create a new array `uniquePeople` that contains only the unique objects based on their name. The resulting `uniquePeople` array contains the objects for Harry, Peter, and Ronnie but only the first occurrence of Harry is included because the iterate function returns the same value for both Harry objects.
// syntax
_.uniq(array, [isSorted], [iteratee])
//example
var people = [
{ name: "Harry", age: 30 },
{ name: "Peter", age: 25 },
{ name: "Harry", age: 35 },
{ name: "Ronnie", age: 30 },
];
var uniquePeople = _.uniq(people, false, function (person) {
return person.name;
});
console.log(uniquePeople);
// [
// { name: "Harry", age: 30 },
// { name: "Peter", age: 25 },
// { name: "Ronnie", age: 30 }
// ]
#javascript #under #score #underscore #underscorejs #uniq #underscoreuniq #uniqfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.difference` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.difference` function on array using JavaScript Library underscore.js
`_.difference` is also utility function in Underscore.js library that allows you to create a new array that contains all the elements from one array that are not present in one or more other arrays.
In below example, we have three arrays `numbers1`, `numbers2` and `numbers3`, and we use `_.difference` to create a new array that contains all the elements from `numbers1` that are not present in any of the other arrays. The resulting `newNumbers` array contains the element 1, because it is the only element in `numbers1` that is not also in `numbers2` or `numbers3`.
Note: `_.difference` does not modify the original arrays, it returns a new array with the excluded values.
// syntax
_.difference(array, [others])
// example
var numbers1 = [1, 2, 3];
var numbers2 = [2, 3, 4];
var numbers3 = [3, 4, 5];
var newNumbers = _.difference(numbers1, numbers2, numbers3);
console.log(newNumbers); // [1]
#javascript #under #score #underscore #underscorejs #difference #underscoredifference #differencefunction #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.every` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.every` function on array using JavaScript Library underscore.js
`_.every` is also utility function provided by the Underscore.js library that allows you to check if every element in an array satisfies a given condition.
In below example, we have an array of `numbers` and we use `_.every` to check if all the elements in the array are even. The `predicate` function returns `true` for even numbers (i.e. numbers that are divisible by 2), and so `_.every` returns `false` because not all elements in the array are even.
Note: `_.every` stops iterating over the array as soon as it encounters the first element that does not satisfy the condition, and returns `false` immediately.
// syntax
_.every(list, predicate, [context])
// example
var numbers = [1, 2, 3, 4, 5];
var allEven = _.every(numbers, function(num) {
return num % 2 === 0;
});
console.log(allEven); // false
#javascript #under #score #underscore #underscorejs #every #underscoreevery #everyfunction #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.reject` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.reject` function on array using JavaScript Library underscore.js
`_.reject` is also utility function provided by the Underscore.js library that is used to filter an array based on a predicate function and returns all elements that do not satisfy the predicate.
Here, `list` is the array to be filtered, `predicate` is the function used to test each element, and `context` is an optional parameter that sets the context of `predicate`. The `predicate` function is called once for each element in the `list`, and should return `true` or `false` to indicate whether the element should be included or excluded from the result.
// syntax
_.reject(list, predicate, [context])
// example
var numbers = [1, 2, 3, 4, 5];
var evens = _.reject(numbers, function(num) {
return num % 2 !== 0;
});
console.log(evens); // [2, 4]
#javascript #under #score #underscore #underscorejs #reject #underscorereject #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
3
views
`_.template` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.template` function on array using JavaScript Library underscore.js
`_.template` is also utility function provided by the Underscore.js library that allows you to create a template that can be rendered with data to produce a string output.
In this example, we create a template string that contains a placeholder for the `name` variable, and then pass it to `_.template`. We then create an object with a `name` property and pass it to the template function, which produces the output string "Hello World!".
var templateString = "Hello <%= name %>!";
var template = _.template(templateString);
var result = template({ name: "World" });
console.log(result); // "Hello World!"
#javascript #under #score #underscore #template #underscoretemplate #object #array #string #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view
`_.reduce` function on array using JavaScript Library underscore.js ( #shorts )
Hi Friends,
`_.reduce` function on array using JavaScript Library underscore.js
`_.reduce` is also utility function provided by the Underscore.js library that applies a given function against an accumulator and each element in an array to reduce it to a single value.
Overall, _.reduce is a very useful function for aggregating and summarizing arrays in JavaScript, and it can be used to perform a wide variety of reduction operations on complex data structures.
var numbers = [1, 2, 3, 4, 5];
var sum = _.reduce(numbers, function(memo, num) {
return memo + num;
}, 0);
console.log(sum); // 15
#javascript #under #score #underscore #reduce #arrayreduce #underscorereduce #object #string #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official
1
view