concat strings from nested objects arrays - javascript

i need to concat all the 'externalId'(inside prod obj) + "id" (inside sup array) + "name" (inside product obj). What would be the best way to do that? I've tried with map and reduce but I wasn't successful. Any help will be appreciated.
const jsonResultKeys = ['AAA', 'BBB', 'CCC']
const items = [];
jsonResultKeys.forEach(k => {
const item = Jsonresult.items[k];
items.push({
description: item.product.name + ':' + item.product.sup[0] + ':'+ item.product.sup[0].prod.externalId ,
})
});
the output expected for this example:
[
{ description: '4444:2:product1'},
{ description: '3333:2:product2'},
{ description: '2222:1:product3'}
]
the json object:
const Jsonresult = {
items: {
'AAA': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: {
sup: [{
prod: {
externalId: **4444**
},
id: **2**
}],
name: "**product 1**"
},
total: 9.84,
quantity: 1,
price: 15,
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
'BBB': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: {
sup: [{
prod: {
externalId: **3333**
},
id: **2**
}],
name: "**product 2**"
},
total: 9.84,
quantity: 1,
price: 15,
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
'CCC': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: {
sup: [{
prod: {
externalId: **2222**
},
id: **1**
}],
name: "**product 3**"
},
}
},
}

The Array#map() method is the most logical method to use - see #MichaelMano's solution - but if you have to use .push() to populate items (const) then stick with .forEach() as follows:
Object.values( Jsonresult.items ).forEach(item => {
items.push( {description: `${item.product.sup[0].prod.externalId}:${item.product.sup[0].id}:${item.product.name}`} );
});
DEMO
const items = [];
const Jsonresult = {
items: {
'AAA': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: {
sup: [{
prod: {
externalId: 4444
},
id: 2
}],
name: "product 1"
},
total: 9.84,
quantity: 1,
price: 15,
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
'BBB': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: {
sup: [{
prod: {
externalId: 3333
},
id: 2
}],
name: "product 2"
},
total: 9.84,
quantity: 1,
price: 15,
updatedAt: '2021-02-11T17:25:22.960-03:00'
},
'CCC': {
createdAt: '2021-02-11T17:25:22.960-03:00',
product: {
sup: [{
prod: {
externalId: 2222
},
id: 1
}],
name: "product 3"
},
}
},
};
Object.values( Jsonresult.items ).forEach(item => {
items.push( {description: `${item.product.sup[0].prod.externalId}:${item.product.sup[0].id}:${item.product.name}`} );
});
console.log( items );

You could do the following, Map over the results and return an object, this will create an array of objects.
You wont even need the keys.
const map = Object.values(Jsonresult.items).map((item) => {
return {
description: `${item.product.sup[0].prod.externalId}:${item.product.sup[0].id}:${item.product.name}`,
};
});
[
{ description: '4444:2:product 1' },
{ description: '3333:2:product 2' },
{ description: '2222:1:product 3' }
]

Related

search an item in multidimentionnal array

I'm trying to find an element on a multidimentionnal array usin JAVASCRIPT function, but I get error
This is my array's data:
export const datas = [
{
id: 1,
firstName: 'John',
tables: [
{ ID: 11, title: 'Lorem' },
{ ID: 12, title: 'Ipsum' },
],
},
{
id: 2,
firstName: 'Doe',
tables: [
{
ID: 22,
title: 'Arke',
nodes: [{ name: 'Name1' }, { name: 'Name2' }, { name: 'Name3' }],
},
{ ID: 23, title: 'Korem' },
],
},
{
id: 3,
firstName: 'Brad',
tables: [
{
ID: 30,
title: 'Mern',
nodes: [{ name: 'Name4' }, { name: 'Name5' }, { name: 'Name6' }],
},
{
ID: 31,
title: 'Full',
nodes: [{ name: 'Name7' }, { name: 'Name8' }, { name: 'Name9' }],
},
],
},
];
I've tried a reccursive function but it's not work, this is my code :
export const findById = (arr, id) => {
for (let o of arr) {
if (o.tables.length > 0) {
let a = findById(o.tables.nodes, 'id');
console.log(a);
}
}
};
I want to print the Object with ID 22, the problem is that I don't have the same structure in each dimension, and it still confuse me..
My Input : 22
My output :
{
ID: 22,
title: 'Arke',
nodes: [{ name: 'Name1' }, { name: 'Name2' }, { name: 'Name3' }],
},
Have you an idea how to edit my function to get my input's response ?
Your recursive function wasn't too far off, you need to check if the item as a tables first before recursively calling it again. And then finally just check the ID in the loop.
eg..
const datas=[{id:1,firstName:"John",tables:[{ID:11,title:"Lorem"},{ID:12,title:"Ipsum"}]},{id:2,firstName:"Doe",tables:[{ID:22,title:"Arke",nodes:[{name:"Name1"},{name:"Name2"},{name:"Name3"}]},{ID:23,title:"Korem"}]},{id:3,firstName:"Brad",tables:[{ID:30,title:"Mern",nodes:[{name:"Name4"},{name:"Name5"},{name:"Name6"}]},{ID:31,title:"Full",nodes:[{name:"Name7"},{name:"Name8"},{name:"Name9"}]}]}];
function findById(arr, ID) {
for (const a of arr) {
if (a.tables) {
const r = findById(a.tables, ID);
if (r) return r;
}
if (a.ID === ID) return a;
}
}
console.log(findById(datas, 22));
if you just need the nested data you can use flatMap and find
const findById = (arr, id) =>
arr
.flatMap(d => d.tables)
.find(t => t.ID === id)
const datas = [{
id: 1,
firstName: 'John',
tables: [{
ID: 11,
title: 'Lorem'
},
{
ID: 12,
title: 'Ipsum'
},
],
},
{
id: 2,
firstName: 'Doe',
tables: [{
ID: 22,
title: 'Arke',
nodes: [{
name: 'Name1'
}, {
name: 'Name2'
}, {
name: 'Name3'
}],
},
{
ID: 23,
title: 'Korem'
},
],
},
{
id: 3,
firstName: 'Brad',
tables: [{
ID: 30,
title: 'Mern',
nodes: [{
name: 'Name4'
}, {
name: 'Name5'
}, {
name: 'Name6'
}],
},
{
ID: 31,
title: 'Full',
nodes: [{
name: 'Name7'
}, {
name: 'Name8'
}, {
name: 'Name9'
}],
},
],
},
];
console.log(findById(datas, 22))
js has amazing array options https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
the ones which will help you most are probably:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap
here are some examples
// get the base with id 22
const baseWith22ID = datas.filter(f => f.tables.filter(s => s.id = 22))
// (i guess you want this one) get all elements with id 22
const onlyElementsWith22ID = datas.flatMap(f => f.tables.filter(s => s.id = 22))

get name of object in array when option.amount equals zero

I'm trying to get an array of names of objects that have options.amount all equals to zero. Thanks for your time. This is what i tryed:
let variants = [
{
name: 'extra',
options: [
{
name: 'meat',
price: 3,
amount: 0
},
{
name: 'cheese',
price: 1,
amount: 0
}
]
},
{
name: 'sauce',
options: [
{
name: 'ketchup',
price: 2,
amount: 1
},
{
name: 'mayo',
price: 1,
amount: 0
}
]
}
];
//Expected output = ['extra']
let arrayOfOptionsNames = variants.map(x => x.options.filter(y => y.amount === 0 ? x.name : 0))
console.log(arrayOfOptionsNames)
You could use Array.prototype.filter() and Array.prototype.every() method to get your result. Every method test if every elements of the array pass the test by the given callback function and returns a boolean value.
const variants = [
{
name: 'extra',
options: [
{
name: 'meat',
price: 3,
amount: 0,
},
{
name: 'cheese',
price: 1,
amount: 0,
},
],
},
{
name: 'sauce',
options: [
{
name: 'ketchup',
price: 2,
amount: 1,
},
{
name: 'mayo',
price: 1,
amount: 0,
},
],
},
];
const ret = variants
.filter((x) => x.options.every((y) => y.amount === 0))
.map((x) => x.name);
console.log(ret);
You could filter and get the wanted property.
let variants = [{ name: 'extra', options: [{ name: 'meat', price: 3, amount: 0 }, { name: 'cheese', price: 1, amount: 0 }] }, { name: 'sauce', options: [{ name: 'ketchup', price: 2, amount: 1 }, { name: 'mayo', price: 1, amount: 0 }] }],
result = variants
.filter(({ options }) => options.every(({ amount }) => !amount))
.map(({ name }) => name);
console.log(result);

Flatten array of objects having arrays nested within their properties

Currently I have an array that contains my clothing (fictitious data):
myClothes = [
{
type: 'shirts',
pieces: [
{
brand: 'patagonia',
quantity: 6,
},
{
brand: 'hugo boss',
quantity: 3,
},
],
},
{
type: 'trousers',
pieces: [
{
brand: 'jack & jones',
quantity: 2,
},
{
brand: 'zara',
quantity: 4,
},
{
brand: 'versace',
quantity: 1,
},
],
},
{
type: 'socks',
pieces: [
{
brand: 'no-name',
quantity: 12,
},
],
},
];
As you see, the array contains objects (shirts, trousers, socks), that seperate the clothing by their type. Now, what can I do with javascript to make a new array of that which looks like this below here?
So where just all pieces are unseperated from shirts/trousers/socks, and it should just show how much articles I have of the brand.
allPiecesUnseperated = [
{
brand: 'patagonia',
quantity: 6,
},
{
brand: 'hugo boss',
quantity: 3,
},
{
brand: 'jack & jones',
quantity: 2,
},
{
brand: 'zara',
quantity: 4,
},
{
brand: 'versace',
quantity: 1,
},
{
brand: 'no-name',
quantity: 12,
},
];
const clothes = [{
type: 'shirts',
pieces: [{
brand: 'patagonia',
quantity: 6,
},
{
brand: 'hugo boss',
quantity: 3,
},
],
},
{
type: 'trousers',
pieces: [{
brand: 'jack & jones',
quantity: 2,
},
{
brand: 'zara',
quantity: 4,
},
{
brand: 'versace',
quantity: 1,
},
],
},
{
type: 'socks',
pieces: [{
brand: 'no-name',
quantity: 12,
}, ],
},
];
const result = clothes.map(({
pieces
}) => pieces).reduce((acc, arr) => [...acc, ...arr], []);
console.log(result);
Use .flatMap() function
const myClothes = [
{
type: 'shirts',
pieces: [
{ brand: 'patagonia', quantity: 6 },
{ brand: 'hugo boss', quantity: 3 }
]
},
{
type: 'trousers',
pieces: [
{ brand: 'jack & jones', quantity: 2 },
{ brand: 'zara', quantity: 4, },
{ brand: 'versace', quantity: 1 }
]
},
{
type: 'socks',
pieces: [
{ brand: 'no-name', quantity: 12 },
]
}
];
const output = myClothes.flatMap(o => o.pieces);
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Probably not the most efficient (since concat does create a new array everytime it is invoked) or the most elegant solution, but should get the job done.
var all = []
myClothes.forEach((item) => {
all = all.concat(item.pieces)
})

Merge Duplicate object in array

I have an array I need to merge duplicate values with the sum of amount.
What would be an efficient algorithm
var arr = [{
item: {
id: 1,
name: "Abc"
},
amount: 1
}, {
item: {
id: 1,
name: "Abc"
},
amount: 2
}, {
item: {
id: 2,
name: "Abc"
},
amount: 2
},{
item: {
id: 1,
name: "Abc"
},
amount: 2
}]
I need solution as
[{
item: {
id: 1,
name: "Abc"
},
amount: 5
}, {
item: {
id: 2,
name: "Abc"
},
] amount: 2
}]
simply use Object.values() with Array.reudce() to merge objects and then get the values:
var arr = [{ item: { id: 1, name: "Abc" }, amount: 1 }, { item: { id: 1, name: "Abc" }, amount: 2 }, { item: { id: 2, name: "Abc" }, amount: 2 },{ item: { id: 1, name: "Abc" }, amount: 2 }];
var result = Object.values(arr.reduce((a,curr)=>{
if(!a[curr.item.id])
a[curr.item.id] = Object.assign({},curr); // Object.assign() is used so that the original element(object) is not mutated.
else
a[curr.item.id].amount += curr.amount;
return a;
},{}));
console.log(result);
used map to catch em all :D
var arr = [{ item: { id: 1, name: "Abc" }, amount: 1 }, { item: { id: 1, name: "Abc" }, amount: 2 }, { item: { id: 2, name: "Abc" }, amount: 2 },{ item: { id: 1, name: "Abc" }, amount: 2 }];
var res = {};
arr.map((e) => {
if(!res[e.item.id]) res[e.item.id] = Object.assign({},e); // clone, credits to: #amrender singh
else res[e.item.id].amount += e.amount;
});
console.log(Object.values(res));

Deleting an object from an array using remove

Given:
var object = {key: value, key1: value, key2: value}
var array = [{object}, {object1}, {object2}, {object3}]
I want to use the parse javascript SDK to delete object 3 and 4 from the array. Using their key2 values. How do I do this?
I believe it goes something like:
object.remove("the key", [object2value2, object3value2])
I need more detail on how to articulate the key and the value. I looked at the docs and I just can't figure it out. I've spent days on this. Humor me, please I'm a newbie and I'm suffering!
THIS IS WHAT SHOWS IN MY TERMINAL AFTER MY PARSE QUERIES WHEN I LIST.GET("OBJECT");
I'd like to delete objects by _id. At the very bottom you see 'false' where I do LIST.REMOVE("_id", [array of _ids]):
[ { _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
_id: 'QllVljV252iNZej9VQgBCYkEyD4Do9fvZMAvmK',
amount: 2307.15,
category: [ 'Shops', 'Computers and Electronics' ],
category_id: '19013000',
date: '2014-06-23',
meta: { location: [Object] },
name: 'Apple Store',
pending: false,
score: { location: [Object], name: 0.2 },
type: { primary: 'place' } },
{ _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
_id: 'NQQVQJVDgDhj90JvnXkMt1jm06eqzji5JvO52Z',
amount: 3.19,
category: [ 'Food and Drink', 'Restaurants', 'Coffee Shop' ],
category_id: '13005043',
date: '2014-06-21',
meta: { location: [Object] },
name: 'Gregorys Coffee',
pending: false,
score: { location: [Object], name: 0.2 },
type: { primary: 'place' } },
{ _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
_id: 'Jwwrw1rnjnfXPvmG9KlZtDoXbQnW1VIMvwrMKp',
amount: 80,
category: [ 'Transfer', 'Withdrawal', 'ATM' ],
category_id: '21012002',
date: '2014-06-08',
meta: { location: [Object] },
name: 'ATM Withdrawal',
pending: false,
score: { location: [Object], name: 1 },
type: { primary: 'special' } },
{ _account: 'mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz',
_id: 'aWWVW4VqGqIdaP495pmetGRqAVKrLRFMD5bMrX',
amount: -240,
category: [ 'Transfer', 'Account Transfer' ],
category_id: '21001000',
date: '2014-06-02',
meta: { location: {} },
name: 'Online Transfer from Chk ...1702',
pending: false,
score: { location: {}, name: 1 },
type: { primary: 'special' } },
{ _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
_id: 'ZnnVnDVbybCqG4DV1BMgCPyAgyDz9vSA2Y5AG1',
amount: 240,
category: [ 'Transfer', 'Account Transfer' ],
category_id: '21001000',
date: '2014-06-01',
meta: { location: {} },
name: 'Online Transfer to Sav ...9606',
pending: false,
score: { location: {}, name: 1 },
type: { primary: 'special' } },
{ _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
_id: 'WOOVOlVrqrHaVDlAdGPmUAKg5k4qBafkZjRkb2',
amount: -0.93,
category: [ 'Interest' ],
category_id: '15000000',
date: '2014-05-17',
meta: { location: {} },
name: 'Interest Payment',
pending: false,
score: { location: {}, name: 0.2 },
type: { primary: 'unresolved' } },
{ _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
_id: '600r0LrVvViXjq96lBpdtyOWboBvzmsaZoeaVz',
amount: 12.74,
date: '2014-05-12',
meta: { location: [Object] },
name: 'Golden Crepes',
pending: false,
score: { location: [Object], name: 0.2 },
type: { primary: 'place' } },
{ _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
_id: 'pQQJQ9J0k0hqAVbDwMmYCrajm2JE6OUNBvwNYa',
amount: 7.23,
category: [ 'Food and Drink', 'Restaurants', 'Coffee Shop' ],
category_id: '13005043',
date: '2014-05-09',
meta: { location: [Object] },
name: 'Krankies Coffee',
pending: false,
score: { location: [Object], name: 0.2 },
type: { primary: 'place' } },
{ _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
_id: '2DD4Dl4nJnCPn4YRJK95hvwgWda5y2SWdDkW6m',
amount: 118.23,
category: [ 'Shops', 'Digital Purchase' ],
category_id: '19019000',
date: '2014-04-26',
meta: { location: {} },
name: 'Banana Republic',
pending: false,
score: { location: {}, name: 0.2 },
type: { primary: 'digital' } },
{ _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
_id: 'oGGNG1NwYwUZQGOB5yjlhYMKG6yMQGtaON9aLd',
amount: -800,
category: [ 'Transfer', 'Third Party', 'Venmo' ],
category_id: '21010001',
date: '2014-04-20',
meta: { location: {} },
name: 'Venmo Cashout 18375552',
pending: false,
score: { location: {}, name: 1 },
type: { primary: 'special' } },
{ _account: 'V66V6EVOpOIVGQEkNpX1HkwDKX0XWLUga5B2Y',
_id: 'pQQJQ9J0k0hqAVbDwMmYCrapBJba4BSNBvwNYk',
amount: 120,
category: [ 'Transfer', 'Third Party', 'Venmo' ],
category_id: '21010001',
date: '2014-04-19',
meta: { location: {} },
name: 'Venmo Payment 16991172',
pending: false,
score: { location: {}, name: 1 },
type: { primary: 'special' } },
{ _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
_id: '055z5gzVyVfzlBnEOqYvcoLL1ZgOWJhkrWMkv2',
amount: 5.32,
category: [ 'Food and Drink', 'Restaurants', 'Coffee Shop' ],
category_id: '13005043',
date: '2014-04-17',
meta: { location: [Object] },
name: 'Octane Coffee Bar and Lounge',
pending: false,
score: { location: [Object], name: 0.2 },
type: { primary: 'place' } },
{ _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
_id: 'LvvrvyrOGOS2e5yE0Bdki45Y1ndVlgfoZ2zoOp',
amount: 28.57,
category: [ 'Food and Drink', 'Restaurants', 'Pizza' ],
category_id: '13005012',
date: '2014-04-11',
meta: { location: [Object] },
name: 'Papa Johns Pizza',
pending: false,
score: { location: [Object], name: 0.2 },
type: { primary: 'place' } },
{ _account: 'mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz',
_id: 'rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61',
amount: -3042.44,
category: [ 'Transfer', 'Payroll' ],
category_id: '21009000',
date: '2014-03-27',
meta: { location: {} },
name: 'Company Payroll',
pending: false,
score: { location: {}, name: 1 },
type: { primary: 'special' } },
{ _account: 'AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj',
_id: '944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB',
amount: 200,
category: [ 'Transfer', 'Withdrawal', 'ATM' ],
category_id: '21012002',
date: '2014-07-21',
meta: { location: [Object] },
name: 'ATM Withdrawal',
pending: false,
score: { location: [Object], name: 1 },
type: { primary: 'special' } },
{ _account: 'AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj',
_id: 'rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK',
amount: 240,
category: [ 'Transfer', 'Account Transfer' ],
category_id: '21001000',
date: '2014-07-24',
meta: { location: {} },
name: 'Online Transfer from External Sav ...3092',
pending: false,
score: { location: {}, name: 1 },
type: { primary: 'special' } } ]
false
The operand to remove needs to equal the object being removed. So first find the object you wish to remove...
var array = myObject.get("theArrayCol");
var removeMe;
for (var i=0; i < array.length; i++) {
if (array[i].key2 == "this one should be removed")
removeMe = array[i];
}
then remove it...
myObject.remove("theArrayCol", removeMe);
EDIT - based on our chat, here's how to apply this in your situation. I broke the code up into simpler functions, each doing an easily definable operation. I hope it makes it easier to understand, and I think its good programming practice anyway...
// token is your key to search the Transaction table in parse
function transactionWithToken(token) {
var query = new Parse.Query("Transactions");
query.equalTo("access_token", token);
query.select("transactions");
return query.first();
}
// array is the value of the array column on the Transaction table
// transactionId is a string that might match the value of the _id property in the array of objects
function transactionInArrayWithId(array, transactionId) {
for (var i=0; i<array.length; i++) {
if (array[i]._id == transactionId) return array[i];
}
return undefined;
}
function removeTransactionWithId(transaction, transactionId) {
var array = transaction.get("transactions");
var t = transactionInArrayWithId(array, transactionId);
transaction.remove("transactions", t);
}
// token is the key to the Transaction table
// transactionIds is an array of ids to remove from the Transaction object's transactions array
function removeTransactionsWithIdsFromToken(token, transactionIds) {
return transactionWithToken(token).then(function(result) {
for (var i=0; i<transactionIds.length; i++) {
removeTransactionWithId(result, transactionIds[i]);
}
return result.save();
});
}
This would be easier to understand if the column name and the table name weren't so similar. Also, underscorejs is great at this sort of array management.
you can try to filter it.
For example if you want to remove all objects which key 'k3' has value of 3;
var obj1 = {k1: 1, k2: 2, k3: 3};
var obj2 = {k1: 4, k2: 5, k3: 6};
var obj3 = {k1: 7, k2: 8, k3: 9};
var array = [obj1, obj2, obj3];
var badValue = 3;
var result = array.filter(function(obj){
return obj.k3 !== badValue;
});

Categories

Resources