I have a array of object like this
AnArray: [
{ name: 'name1',
id: 123456,
arrayOfSomething: [[1], [2], [3]]
},
{ name: 'name2',
id: 123456,
arrayOfSomething: [[0], [2], [1]]
}
I need to push just the arrayOfSomething array in the result array, so I do:
SaveMyResult(){
this.result.push({
something:this.AnArray})
}
but it pushing me all the object data, How can I do ?
If you push AnArray, then yes, the result will be that AnArray is added to the end of your results array. If you don't want that, and you only want one property from each object, use the map method and concatenate the final array it creates:
this.result = this.result.concat(this.AnArray.map(({arrayOfSomething}) => ({arrayOfSomething})));
Here I've used a bit of destructuring to shorten the code, but it's basically going through every element of the array, pulling out its arrayOfSomething property, and replacing the element with a new object that contains only that property.
Related
I have the following structure:
myArr: [
{
key1: 'foo',
myArr: [
{ a: 'blah' }
{ a: 'blah2'}
],
},
{
key1: 'foo2',
}
...More similar entries
];
I am looking for an efficient way to extract the sub arrays into a separate array. The sub arrays have the same key as the parent. The sub arrays may or may not exist.
I can iterate through them in a loop and manually add them to a new array if they exist.
I would prefer to use some sort of map/filter if it's possible to do in this particular case.
The result I am after is:
myArr: [
{ a: 'blah' },
{ a: 'blah2'},
{ a: 'blahN'}
],
A slick way to accomplish this would just be to reduce the initial array:
let result = myArr.reduce((a,b) => a.concat(b.myArr || []), []);
That starts with an empty array and just concats the selected property into it, resulting in a single array of objects. If the subarray is undefined or otherwise falsy, we can concat an empty array (which will not actually add anything to the original array)
This may be a duplicate, but I'm not sure.
I have the following array:
[
{
id: "object1"
},
{
id: "object2"
},
{
id: "object3"
}
]
The trick is, the array is dynamic, and, therefore, the global IDs of this array objects vary. For example, array[1] in one case may be an object with id "object1", and, in other case, that with an id of "object3".
How to query this array based on the id string and have the array index as the output?
reduce into an object indexed by id, with the values being the index of that id's object in the original array, and then you can use simple object lookup:
const input = [
{
id: "object1"
},
{
id: "object2"
},
{
id: "object3"
}
];
const indexedById = input.reduce((a, { id }, i) => {
a[id] = i;
return a;
}, {});
console.log(indexedById.object2); // index of 1 in input
.findIndex is another possibility, but it has worse time complexity than object lookup.
Array has a findIndex, so you could do const findById = (x) => xs.findIndex(({ id }) => id === x) where x is your id string and xs is your array of objects.
Given an array of objects with ids:
array: [
{id: 3, value: 'foo'},
{id: 6, value: 'bar'},
{id: 9, value: 'baz'},
// ...
],
What is the shortest way to return a single object from the array that matches an id?
Keep in mind that the array can be undefined while the model is loading. In that case, the computed property should also return undefined.
This works:
_test : computed.filterBy('array', 'id', 6),
test : computed.alias('_test.firstObject')
But it is not pretty, using a temporary variable.
This is better:
test : computed('array.[]', function() {
let array = this.get('array')
if (array) return array.find(el => el.id == 6)
})
But it is not pretty, because it uses 4 lines.
Ember contains a lot of syntactic sugar, but I haven't figured out how to shrink this.
The methods filter and filterBy will reduce an array to just the list of items you're filtering to.
If you want a single item from the list, use find or findBy.
In your case, you'll use the following.
test: computed('array.[]', function() {
return this.getWithDefault('array',[])
.findBy('id',6)
})
When you're done, this.get('test') will return {id: 6, value: 'bar'}
More information can be found in the documentation for MutableArray
I have defined array of object something like this:
this.choices = [
{
id: 0,
product: [{id:'0'}]
}
];
Now I want to insert new key-value pair in choices :
[
{
id: 10,
product: [{id:'5'}]
}
]
I tried to do it by push() method but I guess its for Array only. Please help me with same. Thank you so much in advance. :)
Also, Is it possible to push these key-pair value at certain index for these array of objects.
This should work,
this.choices.push({id: 10,product: [{id:'5'}]});
Since both examples are actually arrays containing objects, you should be using concat rather than push. ie;
this.choices = [
{
id: 0,
product: [{id:'0'}]
}
];
var newVal = [
{
id: 10,
product: [{id:'5'}]
}
];
this.choices = this.choices.concat(newVal);
In my example, to use push you'd need to do this.choices.push(newVal[0]) - many ways to approach it but basically, push is for individual values, concat is for arrays.
I have array of object and I want to change into array an remove object.
My object is like:
[{ABC: "ELEMENT1", MAX: 2, MIN: "Yes"}, {ABC: "ELEMENT2", MAX: 1, MIN: "Yes"}]
and I want result like array with index:
[{"ELEMENT1",2,"Yes"},{"ELEMENT2",2,"Yes}]
Use Array#map over Object.keys(obj)
The Object.keys() method returns an array of a given object's own enumerable properties.
The map() method creates a new array with the results of calling a provided function on every element in this array.
var ip = {
STU: "Study1",
SUB: 2,
EXL: "Yes"
};
var op = Object.keys(ip).map(function(key) {
return ip[key];
});
console.log(op);
To iterate Array-of-Objects
var ip = [{
STU: "Study1",
SUB: 2,
EXL: "Yes"
}, {
STU: "Study2",
SUB: 4,
EXL: "No"
}];
var op = ip.map(function(item) {
return Object.keys(item).map(function(key) {
return item[key];
});
});
console.log(op);
That would be:
var array = Object.keys(obj).map(key => obj[key]);
However, order is not guaranteed for Object.keys as it works like a for-in loop, whose order of traversal is arbitrary. You may not get ["Study1",2,"Yes"] in that exact order. If you want to guarantee order, you must use an array containing the order of the keys and extract them from your object in that order.
var keyArray = ['STU', 'SUB', 'EXL'];
var array = keyArray.map(key => obj[key]);