Here is a shorter way of achieving it:
let result = objArray.map(a => a.foo);
or
let result = objArray.map(({ foo }) => foo)
let result = objArray.map(a => a.foo);
let result = objArray.map(({ foo }) => foo)
var foo = [1,2,3];
var bar = [1,2,3];
var foo2 = foo;
var bar2 = bar;
foo = [];
bar.length = 0;
console.log(foo, bar, foo2, bar2);
// [] [] [1, 2, 3] []
list = []
assigns a reference to a new array to a variable, while any other references are unaffected. which means that references to the contents of the previous array are still kept in memory, leading to memory leaks.list.length = 0
deletes everything in the array, which does hit other references.a = [1,2,3]; a2 = a;
), and you delete the array’s contents using list.length = 0
, both references (a and a2) will now point to the same empty array. (So don’t use this technique if you don’t want a2 to hold an empty array!)arr.splice(-1,1)
.push
method can take multiple arguments. You can use the spread operator to pass all the elements of the second array as arguments to .push
:>>> a.push(...b)
.apply
instead:>>> a.push.apply(a, b)
>>> Array.prototype.push.apply(a,b)
b
is too long (trouble starts at about 100,000 elements, depending on the browser). If you cannot guarantee that b
is short enough, you should use a standard loop-based technique described in the other answer.Array.filter
:this.persons = this.personService.getPersons().filter(x => x.id == this.personId)[0];
Array.find
this.persons = this.personService.getPersons().find(x => x.id == this.personId);
forEach
function for arrays.//this sets all product descriptions to a max length of 10 characters
data.products.forEach( (element) => {
element.product_desc = element.product_desc.substring(0,10);
});
for(let i=0; i<data.products.length; i++){
console.log(data.products[i].product_desc); //use i instead of 0
}
Mã truy cập dài hạn https://developers.facebook.com/docs/facebook-login/guides/access-tokens/get-long-lived/ https://community.n8n.io/t/re...