Add a property with .map() in rxjs - javascript

If I had an object car:
{
make: "Chevy",
model: "Sonic"
type: {
id: 1,
name: "Tiny"
}
}
And I wanted to manipulate the object to add a typeId property:
{
make: "Chevy",
model: "Sonic"
type: {
id: 1,
name: "Tiny"
},
typeId: 1
}
Which rxjs operator would you use, assuming you had an array of these objects? This is what I'm trying to avoid:
someArray.map(item => ({
make: item.make,
model: item.model,
type: item.type,
typeId: item.type.id
}));

To add a property in each object in the array, you could use forEach as you don't have to map the array
someArray.forEach(o => o.typeId = o.type.id)

Related

how To turn an json object to table of objects

i used to push a data into array but the data was like bellow can some one tell me how to turn this object to arrray of object
d2 = {
{
id: "BK1",
type: "MAGE",
role: "DARK"
},
{
id: "BK1",
type: "MAGE",
role: "DARK"
},
{
id: "BK1",
type: "MAGE",
role: "DARK"
}
}
to this how i want it to look like:
d2 = [
{
id: "BK1",
type: "MAGE",
role: "DARK"
},
{
id: "BK1",
type: "MAGE",
role: "DARK"
},
{
id: "BK1",
type: "MAGE",
role: "DARK"
}
]
You can use a for ... in loop to iterate over the keys within the object and push those onto an array:
const transformedD2 = [];
for (const key in d2) {
transformedD2.push(d2[key]);
}
You could also use Object.values
The thing is, the object you included as an example isn't valid, as briosheje has pointed out. It only has values, no keys.

Mongoose find by id ignoring depth level

I have this schema
const personSchema = new Schema({
name: {
type: String,
required: true
},
children: {
type: Array,
default: [],
required: false
}
}, { timestamps: true });
Inside children I can have another person object.
ex:
{ name: 'yoshi',
id: 01,
children: [
{ name: 'luigi',
id: 02,
children: []
},
{ name: 'mario',
id: 03,
children: [
{ name: 'bowser',
id: 04
children: []
}
]
If I only have 'bowser' ID (04), how to get 'bowser' object and it's children?
I try Person.findById(04), but I can't get it.
I only can get yoshi (01) using findById
Is there a way to ignore all level and focus only getting by ID?
I dont want to use find(children.children.id), because I have dynamic depth level.

Remove multiple object from array Javascript [duplicate]

This question already has answers here:
Looping through array and removing items, without breaking for loop
(17 answers)
Closed 2 years ago.
I am trying to delete the all the objects with _id "bennyRawMaterial" from below details array nested in the object:
let recipeBasicRecipes = [{
_id:'12345',
name:'Macaron Shell',
details:[
{
name: 'Sugar',
_id: 'bennyRawMaterial',
type: 'solid',
}
,
{
name: 'Egg white',
_id: '5fef680ca43301322a3224e5',
type: 'solid'
}]
},
{
_id:'14512345',
name:'Macaron Shell',
details:[{
name: 'Castiors gar',
_id: 'bennyRawMaterial',
type: 'solid'
},
{
name: 'oil',
_id: 'bennyRawMaterial',
type: 'solid',
}
, {
name: 'food',
_id: 'bennyRawMaterial',
type: 'solid',
}]
}]
I am using following code to remove the objects, but it skips few of the objects.Please help with the implementation
recipeBasicRecipes.forEach(br => {
br.details.forEach((rm, index) => {
if (rm._id === 'bennyRawMaterial') {
br.details.splice(index, 1);
} else {
return true;
}
});
maintain a global ids array,
and go through the details array of each object
check the id exist in global ids array
let recipeBasicRecipes = [{
_id: '12345',
name: 'Macaron Shell',
details: [{
name: 'Sugar',
_id: 'bennyRawMaterial',
type: 'solid',
},
{
name: 'Egg white',
_id: '5fef680ca43301322a3224e5',
type: 'solid'
}
]
},
{
_id: '14512345',
name: 'Macaron Shell',
details: [{
name: 'Castiors gar',
_id: 'bennyRawMaterial',
type: 'solid'
},
{
name: 'oil',
_id: 'bennyRawMaterial',
type: 'solid',
}, {
name: 'food',
_id: 'bennyRawMaterial',
type: 'solid',
}
]
}
]
var uniqueIds = []; //global ids array
recipeBasicRecipes.forEach(el => {
let details = [];//unique details array
el.details.forEach((dt, i) => {
let id = dt._id;
if (!uniqueIds.includes(id)){ //check id exists in global ids array
uniqueIds.push(id);
details.push(dt); //copy unique details
}
});
el.details = details; //update details with unique details
});
console.log(recipeBasicRecipes)

how to create dynamic multi-dimensional array of objects with in javascript

I have an array of objects that has information about cars. I want to grouping on categoryId
var car = [
{ category: 1, model: "bmw" },
{ category: 1, model: "benz" },
{ category: 1, model: 'ford' }
{ category: 2, model: "kia" },
{ category: 2, model: "fiat" },
{ category: 3, model: "mg" },
];
I want this result
[
[
{ category: 1, model: 'bmw' },
{ category: 1, model: 'benz' },
{ category: 1, model: 'ford' }
],
[
{ category: 2, model: 'kia' },
{ category: 2, model: 'fiat' }
],
[
{ category: 3, model: 'mg' }
]
]
this is my solution but I want a way for this result based on reduce or ... I dont want to use if in forEach
let groupedCars = [];
cars.forEach((car) => {
if (!groupedCars[car.category]) {
groupedCars[car.category] = [];
}
groupedCars[car.category].push(car);
});
Solution using Array.prototype.reduce() method. Traverse the array and group it by category using reduce and at last, use Object.values() to get the result.
const car = [
{ category: 1, model: 'bmw' },
{ category: 1, model: 'benz' },
{ category: 1, model: 'ford' },
{ category: 2, model: 'kia' },
{ category: 2, model: 'fiat' },
{ category: 3, model: 'mg' },
];
const ret = Object.values(
car.reduce((prev, c) => {
const p = prev;
const key = c.category;
p[key] = p[key] ?? [];
p[key].push({ ...c });
return p;
}, {})
);
console.log(ret);
Not clear what you mean by result based on ES6. But, you could try using reduce method.
NOTE: Javascript arrays start with index 0 so, you might need to add some logic in your code to fix that

Construct an array of objects after doing a filter for an array in JavaScript

I was able to filter the array but when I'm trying to create an array of objects out of the filtered data, the result appears to be undefined. How do I construct an array of objects in the below format. Could anyone please help?
[{ brand: 'BMW'}, { brand: 'Audi'}]
Snippet
const cars = [{
name: 'BMW',
type: 'Sedan'
}, {
name: 'Audi',
type: 'SUV'
}, {
name: 'BMW',
type: 'SUV'
}]
const result = cars.filter(({
type
}) => type === 'SUV').map((car) => {
brand: car.name
})
console.log(result)
If you want to return an object literal from the arrow function, you need to enclose that object literal in parentheses to distinguish it from a code block, which also happens to be enclosed in curly braces:
result = cars.map(car => ({
brand: car.name
}));
It's funny that your code doesn't cause an error. It's just because there's a label syntax in JavaScript, so your code inside the arrow function basically creates a brand label to a loose value of car.name.
Basically you need to wrap the object in parentheses to distinguish it from a block statement.
const
cars = [{ name: 'BMW', type: 'Sedan' }, { name: 'Audi', type: 'SUV' }, { name: 'BMW', type: 'SUV' }],
result = cars
.filter(({ type }) => type === 'SUV')
.map(({ name: brand }) => ({ brand }));
// ^^^^^^^^^^^ wrap it
console.log(result);
You are missing a pair of parenthesis around the new implicitly returned object from the map function. This is a tricky syntax of es6.
const cars = [{
name: 'BMW',
type: 'Sedan'
}, {
name: 'Audi',
type: 'SUV'
}, {
name: 'BMW',
type: 'SUV'
}]
const result = cars.filter(({
type
}) => type === 'SUV').map((car) => ({
brand: car.name
}))
console.log(result)
For doing this you can declare a variable and return it.
const cars = [{
name: 'BMW',
type: 'Sedan'
}, {
name: 'Audi',
type: 'SUV'
}, {
name: 'BMW',
type: 'SUV'
}]
const result = cars.filter(({
type
}) => type === 'SUV').map((car) => {
let obj = {brand: car.name}
return obj
})
console.log(result)

Categories

Resources