Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a SortableList where you can organize your order to make filter priorities.
like that:
[
0: "Fruits",
1: "Legumes",
2: "Example1",
]
then I have some data like this:
[
{
id: 0,
_source: {
someOtherProps,
source: "Fruits"
}
},
{
id: 1,
_source: {
someOtherProps,
source: "Example1"
}
},
.... blablabla
]
I want to sort the data by the source, respecting the order priorities.
I try to use the sort() function, but I don't have any ideas how I could do that.
In this example: I will have all my data object sorted firstly by the source "Fruits", then "Legumes" then "Example1".
You could use an object for the sort order and take the delta as result for the callback.
var order = { Fruits: 1, Legumes: 2, Example1: 3 },
data = [{ id: 0, _source: { source: "Fruits" } }, { id: 1, _source: { source: "Example1" } }];
data.sort((a, b) => order[a._source.source] - order[b._source.source]);
console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
I want to convert this:
data: {
"3": {
name: ["Missing data for required field."],
},
"5": {
id: ["Same id exist."],
},
}
into this:
data: [{
key: "name",
message: "Missing data for required field.",
row: 3,
},
{
key: "id",
message: "Same id exist.",
row: 5,
},]
how to achieve this with just es6 syntax
You can do this with Object.entries:
const data = {
"3": {
name: ["Missing data for required field."]
},
"5": {
id: ["Same id exist."]
}
};
Object.entries(data).map(([a, b]) => {
return {
key: Object.keys(b)[0],
message: Object.values(b)[0][0],
row: a
};
});
I've made some assumptions:
There will only be one key in each object
The value of the key will be an array with one string
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Group javascript array items on value
Assuming you have a json object like:
[
{
prNumber: 20000401,
text: 'foo'
},
{
prNumber: 20000402,
text: 'bar'
},
{
prNumber: 20000401,
text: 'foobar'
},
]
Is it possible to perform a "join" on prNumber?
For example, maybe the desired output would be something like:
[
{
prNumber: 20000401,
text: [
'foo',
'foobar'
]
},
{
prNumber: 20000402,
text: [
'bar'
]
}
]
I have no code samples worth anything, so I will not post them here.
This would preferrably use vanilla javascript, but will accept a jQuery answer.
You should iterate the initial array and create new objects keyed off the prNumber. Here's how to do it using reduce (assuming you've assigned the array to a variable named orig):
var result = orig.reduce(function(prev, curr, index, arr) {
var num = curr["prNumber"];
if (!prev[num]) {
prev[num] = [];
}
prev[num].push(curr["text"]);
return prev;
}, {});
You can easily convert this into the example structure outlined in your question.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have database which is array of objects. It look like this:
export const BIKES: Bike[] = [
{
id: 1,
imgUrl:
'https://cyclingmagazine.ca/wp-content/uploads/2018/10/Krypton_A18_2016_red_ultegra_16_1.jpg',
price: 28000,
discount: 71,
main: true,
shop: 'Canada Bike',
name: 'Argon 18',`
},
{
id: 2,
imgUrl:
'https://cyclingmagazine.ca/wp-content/uploads/2018/03/etap.shadowpsd_2048x.png',
price: 7900,
discount: 41,
main: false,
shop: 'Amazon',
name: 'Aquila Cycles',
}
I want to subscribe to array of objects which will consist of 2 properties 'name' and 'shop' and theirs values.
It can be implemented using Array.map
const result = BIKES.map(({name, shop}) => ({ name, shop }));
console.log(result);
or
const result = BIKES.map((item) => {
return {
name: item.name,
shop: item.shop
};
});
console.log(result);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a Object in this format.
const obj = [{
name: 'name',
items: [{
name: 'name 1',
items: [
{ value: 100 },
{ value: 50 }
]
}]
}]
Imagine that all arrays repeat several times in length and an object inside another.
How I can iterate all objects in all arrays and check if all have the property "name" and "items", if it doesn't have check if has the property "value".
I trying to make a recursion but I don't no how to check if the last "items" has a value to stop and continue to check the rest.
Thanks everyone.
You can use Object.prototype.hasOwnProperty to check if an object has a specific property and Array.prototype.flatMap to map the inner arrays to a single array.
const obj = [{
name: 'name',
items: [{
name: 'name 1',
items: [
{ value: 100 },
{ value: 50 },
{
name: 'name 2',
items: [{
value: 150
}]
}
]
}]
}];
function getValues(arr) {
return arr.flatMap(entry => {
if (entry.hasOwnProperty('name') && entry.hasOwnProperty('items')) {
return getValues(entry.items);
}
return entry.value;
});
}
console.log(getValues(obj));
I resolve my question with:
function check(objs) {
for (let obj of objs) {
if(!('name' in obj) &&
!('value' in obj))
throw new Error('invalid')
if('name' in obj)
check(obj.items)
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have data like:
var data = [
{
items: [
{
id: 123
},
{
id: 234
},
{
id: 123
}
]
}, {
items: [
{
id: 123
},
{
id: 234
}
]
}
]
so, I want count object deep in array inside of all data by property 'id'.
ex: data.countObject('id',123) //return 3.
and my data have about xx.000 item, which solution best?
Thanks for help (sorry for my English)
You can use reduce & forEach. Inside the reduce callback you can access the items array using curr.items where acc & curr are just parameters of the call back function. Then you can use curr.items.forEach to get each object inside items array
var data = [{
items: [{
id: 123
},
{
id: 234
},
{
id: 123
}
]
}, {
items: [{
id: 123
},
{
id: 234
}
]
}];
function getCount(id) {
return data.reduce(function(acc, curr) {
// iterate through item array and check if the id is same as
// required id. If same then add 1 to the accumulator
curr.items.forEach(function(item) {
item.id === id ? acc += 1 : acc += 0;
})
return acc;
}, 0) // 0 is the accumulator, initial value is 0
}
console.log(getCount(123))