I am trying to get my function working and I can not for the life of me figure out why it isnt. I am trying to use .filter() to search the array of objects to find the object with the tag ketchup. Then return the whole object it is in to the console log
let foodArr = [
{
type: 'Chicken',
rating: 1,
tags: ['chicken', 'free-range', 'no hormones'],
price: 10,
popularity: 80
},
{
type: 'pizza',
rating: 5,
tags: ['pepperoni', 'sauce', 'bread'],
price: 25,
popularity: 56
},
{
type: 'hamburger',
rating: 3,
tags: ['bun', 'patty', 'lettuce'],
price: 8,
popularity: 99
},
{
type: 'wings',
rating: 4,
tags: ['wing', 'bbq', 'ranch'],
price: 12,
popularity: 68
},
{
type: 'fries',
rating: 2,
tags: ['ketchup'],
price: 4,
popularity: 100
}
]
const filteredFood = foodArr.filter(function(obj) {
return obj.tags[''] === 'ketchup'
})
console.log(filteredFood)
const filteredFood = foodArr.filter(function(value){
for (let i=0; i<value.tags.length; i++)
if(value.tags[i] === 'ranch'){
return value.tags[i]
}
}
)
console.log(filteredFood)
I am trying to find the reactive way of manipulating data in an array of objects. In the array below, I want to determine the low score in groups of four and add a low and diff property to the item.
arr = [
{ name: 'Cliff', score: 44 },
{ name: 'Bob', score: 55 },
{ name: 'Jack', score: 404 },
{ name: 'John', score: 50 },
{ name: 'Doug', score: 22 },
{ name: 'EZ', score: 550 },
{ name: 'Bill', score: 404 },
{ name: 'Dan', score: 5 },
{ name: 'Rich', score: 404 },
{ name: 'Steve', score: 565 },
];
arr$: Observable<Arr[]> = of(this.arr);
getData() {
const scores = this.arr$.pipe(
map((result) =>
result.map((data) => {
return {
x: data.score,
};
})
)
// reduce((acc, x) => (acc = acc > x.score ? acc :x.score), 0)
);
return scores;
}
}
/*
Desired result
arr = [
{ name: 'Cliff', score: 44, low: 44, diff 0 },
{ name: 'Bob', score: 55, low: 44, diff: 9 },
{ name: 'Jack', score: 404, low 44, diff: 360 },
{ name: 'John', score: 50, low: 44, diff: 6 },
{ name: 'Doug', score: 22, low 5, diff: 17 },
{ name: 'EZ', score: 550, low 5, diff: 545 },
{ name: 'Bill', score: 404, low 5, diff: 399 },
{ name: 'Dan', score: 5, low 5, diff: 0 },
{ name: 'Rich', score: 404, low: 404, diff: 0 },
{ name: 'Steve', score: 565. low 404, diff: 121 },
];
*/
I can't seem to get past the few lines of code that I've written. A StackBlitz is here.
This could be a reactive way to get what you want. The comments in the code explain the logic.
// the from rxjs function takes an array and returns an
// Observable that emits each item in the array
from(arr)
.pipe(
// bufferCount will emit an array of arrays of items,
// therefore creates the groups of four items
bufferCount(4),
// for each group we find the low and create
// the new items with the low value and the diff
map((buff) => {
const low = buff.reduce((acc, curr) => {
return acc < curr.score ? acc : curr.score;
}, Infinity);
return buff.map((v) => ({ ...v, low, diff: v.score - low }));
}),
// with mergeMap we flatten the groups of items
// and emit each item
mergeMap((buffEnriched) => buffEnriched),
// if you want to emit an array rather than each single
// object you add toArray
toArray()
)
it makes more sense to sort, then generate your data. otherwise, your output data in your question makes little sense to me.
// DATA IN
arr = [
{ name: 'Cliff', score: 44 },
{ name: 'Bob', score: 55 },
{ name: 'Jack', score: 404 },
{ name: 'John', score: 50 },
{ name: 'Doug', score: 22 },
{ name: 'EZ', score: 550 },
{ name: 'Bill', score: 404 },
{ name: 'Dan', score: 5 },
{ name: 'Rich', score: 404 },
{ name: 'Steve', score: 565 },
];
// PROCESSING and OUTPUT
let lowest = arr[0].score;
let output_arr = arr.sort((a, b)=>{
if(a.score < b.score) return -1;
if(a.score > b.score) return 1;
return 0;
}).map((a)=>{
if(a.score < lowest) lowest = a.score;
return {
name: 'Steve',
score: 565,
low: lowest,
diff: a.score - lowest
};
});
// use output array output
console.log(output_arr);
DEMO
Example
INPUT
[
{ name: 'Cliff', score: 44 },
{ name: 'Bob', score: 55 },
{ name: 'Jack', score: 404 },
{ name: 'John', score: 50 },
{ name: 'Doug', score: 22 },
{ name: 'EZ', score: 550 },
{ name: 'Bill', score: 404 },
{ name: 'Dan', score: 5 },
{ name: 'Rich', score: 404 },
{ name: 'Steve', score: 565 },
]
OUTPUT
[{
diff: 0,
low: 5,
name: "Steve",
score: 565
}, {
diff: 17,
low: 5,
name: "Steve",
score: 565
}, {
diff: 39,
low: 5,
name: "Steve",
score: 565
}, {
diff: 45,
low: 5,
name: "Steve",
score: 565
}, {
diff: 50,
low: 5,
name: "Steve",
score: 565
}, {
diff: 399,
low: 5,
name: "Steve",
score: 565
}, {
diff: 399,
low: 5,
name: "Steve",
score: 565
}, {
diff: 399,
low: 5,
name: "Steve",
score: 565
}, {
diff: 545,
low: 5,
name: "Steve",
score: 565
}, {
diff: 560,
low: 5,
name: "Steve",
score: 565
}]
var arr = [
{ id: 1, name: 'Ahmed Malick', school: 'TEWGS' },
{ id: 2, name: 'Tehmeed Anwar', school: 'DGS' },
{ id: 3, name: 'Azhar Yameen', school: 'DGS' }
]
I want this output:
The student name is his id is and he studies in
Can you please show me what kind of output you expect. Then i will try to solve it.
I'm not sure if this is what you want
var arr = [
{ id: 1, name: "Ahmed Malick", school: "TEWGS" },
{ id: 2, name: "Tehmeed Anwar", school: "DGS" },
{ id: 3, name: "Azhar Yameen", school: "DGS" },
];
arr.map((student) => {
return `Name: ${student.name}, id: ${student.id}, he studies in: ${student.school}`;
}).forEach((output) => {
console.log(output);
});
If you want it in the DOM do this
let html = arr.map((student) => {
return `<p><strong>Name</strong>: ${student.name}, <strong>id</strong>: ${student.id},<strong> he studies in</strong> ${student.school}</p>`;
}).join("")
document.createElement("div").innerHTML = html
Try thatGood luck
I have a object array in which each object contain an id and a name and a separate array contains a set of ids. I want to filter first array based on the second array.
const data= [
{
id: 1,
name: 'name1'
},
{
id: 2,
name: 'name2'
},
{
id: 3,
name: 'name3'
},
{
id: 4,
name: 'name4'
}
];
const array = [1,3,4];
const expectedResult= [
{
id: 1,
name: 'name1'
},
{
id: 3,
name: 'name3'
},
{
id: 4,
name: 'name4'
}
];
Use .filter and .includes
const data= [
{
id: 1,
name: 'name1'
},
{
id: 2,
name: 'name2'
},
{
id: 3,
name: 'name3'
},
{
id: 4,
name: 'name4'
}
];
const array = [1, 3, 4]
const result = data.filter((item) => {
//gives us items that passes a condition
return array.includes(item.id)
})
console.log(result)
I have the following arrays:
arr1 = [{
id: 1,
name: 'Diego',
age: 23
}, {
id: 2,
name: 'Brian',
age: 18
}]
arr2 = [{
id: 1,
name: 'Diego',
age: 23
}, {
id: 2,
name: 'Brian',
age: 18
}, {
id: 3,
name: 'Pikachu',
age: 88
}]
I need get difference between this two arrays, the espected result is:
arr3 [{id:3, name: 'Pikachu', age: 88}]
How do i solve this problem using ES6 or TypeScript?
I tried using SET, but doesn't worked.
Something like this maybe:
let ids1 = arr1.map(item => item.id);
let ids2 = arr2.map(item => item.id);
let diff = ids1.map((id, index) => {
if (ids2.indexOf(id) < 0) {
return arr1[index];
}
}).concat(ids2.map((id, index) => {
if (ids1.indexOf(id) < 0) {
return arr2[index];
}
})).filter(item => item != undefined);
(code in playground)