Increment array object duplicate keys by 1 in javascript - javascript

This is my array
[
{Product Name: "yafuyafu"},
{Parameter: "sss"},
{Description: "uyghik"},
{Parameter: "some"},
{Description: "YogaYoga"}
]
The key Parameter and Description appears twice here and I want it to be like this
[
{Product Name: "yafuyafu"},
{Parameter: "sss"},
{Description: "uyghik"},
{Parameter1: "some"},
{Description1: "YogaYoga"},
{Parameter2: "some"},
{Description2: "YogaYoga"}
{Parameter3: "some"},
{Description3: "YogaYoga"}
]
I populate this Select with the Keys of my array objects
This select would not add the duplicate Parameter and description keys and i believe it is because of the name and besides that i want the user to be able to differentiate them.
Please can someone help me figure this out, thank you very much in advance.

Related

How to merge arrays that derive from the same variable

I have the following line of code which returns me one or more arrays depending on the checkbox that is clicked.
selected.forEach(langsel => {
let filtered = person.filter(pers => pers.language == langsel);
})
selected and I do not report the other variables for simplicity in reading.
I have a list of checkboxes where each refers to a particular language. So every time a checkbox is clicked I want the people who speak that language to be returned to me; this must be returned in an array of objects.
catsel refers to the checkbox that is selected (being a checkbox, more than one can also be selected). So for each language that is selected it returns me the array of objects in the filtered variable.
For example, if I select the English language through the checkbox, I get:
[{id: "2", name: "Tomas Addreh", language: "English"},{id: "6", name: "Mark Addreh", language: "English"}];
if together with the checkbox selected previously, therefore English, I also select the checkbox relating to the Spanish language, filtered returns me:
[{id: "2", name: "Tomas Addreh", language: "English"},{id: "6", name: "Mark Addreh", language: "English"}];
[{id: "15", name: "Alex Atres", language: "Spanish"}, {id: "1", name: "Mark Sertoj", language: "Spanish"}, id: "12", name: "Martha Forrest", language: "Spanish"];
filtered in the latter case returns me two separate arrays.
I want them to be merged into an array.
Can anyone kindly help me?
I concat the array by using spread operator.
let people = [];
selected.forEach(langsel => {
let filteredPerson = person.filter(pers => pers.language == langsel);
people = [...people, ...filteredPerson];
})

Sequelize.js get orders divided by the status

I have a Node.JS project and am using sequelize as the ORM.
I would like to run a query where I get all of my orders grouped by the status. So I would get for example an object with 5 keys (given that I have 5 different statuses). And in each key there would be an array with all orders that have that status.
Example of object:
{
"Done": [{id:1, item: "bag"}, {id:2, item: "purse"}],
"Processing": [{id:3, item: "bag"}, {id:4, item: "purse"}],
"Pending": [{id:5, item: "bag"}, {id:6, item: "purse"}]
}
Is this possible in any way or I need to get all possible statuses and just run the same number of queries each time changing the where clause?
Thanks
.findAll() gives you back a result set consisting of an array of model instances. I guess in your case it will be an array of Orders. It's flattened, not hierarchical.
That will look something like this.
[
{status: "Done", id:1, item: "bag"},
{status: "Done", id:2, item: "purse"},
{status: "Processing", id:3, item: "bag"},
{status: "Processing", id:4, item: "purse"},
{status: "Pending", id:5, item: "bag"},
{status: "Pending", id:6, item: "purse"}
]
You can then turn that flattened result set into the hierarchical one you want. Something like this, not debugged, should do the job.
const byStatus = {}
for (const row of resultSet) {
/* first time we've seen this status value? if so create an array for it. */
if (!byStatus[row.status]) byStatus[row.status] = []
/* put the present row into the appropriate array */
byStatus[row.status].push( {id: row.id, item: row.item })
}
That way you can run just one .findAll() query. That's a good idea because it's more efficient than running multiple ones.
You need to use a "native" query with a GROUP BY clause or you fetch the data normally and do the gruoping in JS/TS using lodash's groupBy.

nested json data minipulation for ngx datatable in angualr -6 / js

I am trying to create a ngx datatable that creates columns dynamically from nested arrays, which with some research is not possible - so to achieve my desired result, I must flatten my nested arrays with the key / values that i need from each nested object into my parent object.
I need to manipulate my data so that my end result is a flat array and contains a line item for each object in the nested array earnings with 'abbreviation' being the key and 'amount' being the value..
I.e
[
{
employee_uuid: 978f37df-7e07-4118-be93-d82507ce5c46,
employee_code: JB00024,
full_name: Thulisile Sandra,
last_name: Bhekiswayo
earnings:[{
abbreviation: "NT HRS"
amount: "45.00"
money: false
name: "Normal Time HRS"
time: true
unique: "7d783469-717e-408a-bc3c-93115cb632dd_true"
uuid: "7d783469-717e-408a-bc3c-93115cb632dd"
value: "45.00"
},
{
abbreviation: "OT HRS"
amount: "25.00"
money: false
name: "Normal Time HRS"
time: true
unique: "7d783469-717e-408a-bc3c-93115cb632dd_true"
uuid: "7d783469-717e-408a-bc3c-93115cb632dd"
value: "45.00"
}],
terminated false
}
...
]
I'd like to look like this:
[
{
employee_uuid: 978f37df-7e07-4118-be93-d82507ce5c46,
employee_code: JB00024,
full_name: Thulisile Sandra,
last_name: Bhekiswayo,
NT HRS: '45.00',
OT HRS, '25.00',
terminated:false
}
...
]
I am not sure how to go about this, I've tried reducing and map functions but no success.. I can add the nested arrays to the parent object with Object.assign but that takes the whole object, I need to create a new parameter from that object..
Any help would be hugely appreciated..
You can use es6 destructuring for this, simply expose whatever properties you need and then "recompose" then into the object shape you want.
For example:
return myEmployeeArray.map(employee => {
const {earn } = employee
earn.map(field => field.abbreviation)
myDesiredObject = { fieldA: employee.abbreviation....fieldE:field.abbreviation}
}
This would get you one of your nested fields

Combine 2 different javascript json objects into one with a loop and joining on the questionid ,

So I am trying to combine 2 different javascript object than I can end up using in an angularjs ng-repeat . since there is a 1 to many relationship from the database, I was pulling queries separately.
What I have is this:
Main list of data , 3 object sample
0: Object
$$hashKey: "object:4"
Active:true
QuestionId:2
SalesChannel:"DTD"
1: Object
$$hashKey: "object:5"
Active:true
QuestionId:3
SalesChannel:"DTD"
2: Object
$$hashKey: "object:6"
Active:true
QuestionId:5
SalesChannel:"DTD"
Then another query returned data into what I want to relate as JSON into the other object
Object { Id: 3, Name: "Text box" QuestionId: 3}
{ Id: 4, Name: "Text box" QuestionId: 3}
{ Id: 9, Name: "Text box" QuestionId: 5}
So since I have both of these objects , I am wanting to combine.
Naturally I would think that I should return from the database, but then I also think about looping over and appending
for loop on main
{
.... find where main.QuestionId = sub.QuestionId and it to be added in as a nested object of json type...
Thus the end result SHOULD look like
[{
Active:true,
QuestionId: 3
SubData: [ {
Id: 3,
Name: "TextBox
QuestionId:3
},
{
Id: 4,
Name: "TextBox
QuestionId:3
}],
SalesChannel: "DTD"
}]
// and so on
How can i achieve this?
You want to go through all of the main objects, and assign to them only those results from second query. Use Array.prototype.forEach to go through them all, and Array.prototype.filter to select only appropriate results from secondary query.
firstQueryResult.forEach((firstObject)=>
firstObject.subdata = secondQueryResult.filter((secondObject)=>
secondObject.QuestionId === firstObject.QuestionId))
BTW, this has nothing to do with angularjs
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/forEach

Angular Select initial value not selected

I have a strange problem with data-binding on select.
Here are the definitions of my objects :
brands =
[
{
$$hashKey: "object:18"
firm: Object
id: 242
name: " CONTAGEL "
regex: null
},
...
]
products =
[
{
$$hashKey: "object:7"
brand:
{
$$hashKey: "object:613"
firm: Object
id: 32
name: "Level Junior Solaire"
regex: null
}
label: "Level Junior Solaire - LL LEJUS1501"
productCode: "01646554"
},
...
]
I have a list of brand and a list of products. Each product have a brand (pulled from the brand array).
I want to display the list of product with a select for changing the brand.
<div ng-repeat="p in products">
Product : {{p.productCode}}
<select ng-model="p.brand" ng-options="b as b.name for b in brands">
</div>
The option list is filled with the brand list but not any value is selected. And when i change the value of the select, it changes the brand of the product.
So I don't understand what I have missed.
The issue is with your ng-options. ng-model on select boxes uses references, so unless the object is the same reference, it's not going to be selected by default. In your case, it's a new object and therefore not the same reference, so in ng-model's eyes, it's not a match. Use track by to match on the id.
ng-options="b.name for b in brands track by b.id"
The brand object from Brands variable is different from the brand object of the products variable as you can see in your example.

Categories

Resources