Filtering in Object of Arrays in Javascript - javascript

I am trying to filter out an Object of Arrays wherein I have to filter out the 'designation' 'options' of a key.
My row Object looks like.
{id: 1, columns: Array(5)}
columns: (5) [InputDate, InputSelect, InputSelect, InputSelect, InputString]
id: 1
__proto__: Object
One of the columns Array where data is looks like this.
InputSelect
name: "designation"
options: Array(3)
0: {name: "Entry", value: "ENTRY"}
1: {name: "Mid", value: "MID"}
2: {name: "Experienced", value: "EXPERIENCE"}
length: 3
__proto__: Array(0)
Now I have to search inside the columns Arrays and filter out the options of "ENTRY" if the Arrays name property is 'designation' so as to return the updated Object having this filtered value.
What I am trying now is
row.columns.map( key => {
if (key.name === 'designation') {
key.options.filter( r => r.value === "ENTRY" )
}
But it doesn't seem to be updating the row Object.

This should solve your problem.
row.columns.filter(x=>x.name=="designation").map(i=>i.options.filter(z=>z.name==="Entry"));

Related

Add values to mapped array

I have an array with this values:
0:
code: "fb"
description: "Not relevant text."
did: 1
name: "Some random name"
sortOrder: 1
1:
code: "fr"
description: "Not relevant text."
did: 2
name: "Some random name"
sortOrder: 2
When i map the array like this:
values: this.stackOverflowExample.map(v => v.code).push(null)
And push a null code to the set, it sets all values to null
The output I want:
values: [
0: 'fb'
1: 'fr'
2: null ]
The output I got:
values: 3
How do I add values to the array this.stackOverflowExample
which is mapped by code, without affecting the other values?
The push method returns the new length of the array and not the mutated array, see documentation.
If you just want to append null to the mapped array you could either:
Concat your mapped array with [null]
values: anArray.map(mapFn).concat([null])
Spread your mapped array in an array with the last item being null
values: [...anArray.map(mapFn), null]

Need to remove object from javascript by key

I have an array:
(2) [{…}, {…}]
0: {code: "sku", label: "SKU", value: "Number: 312312"}
1: {code: "show_more_options", label: "Show More Options", value: "New variant!"}
length: 2
__proto__: Array(0)
getting from variable order.attributes_list.
i need to remove from that element that has code === "sku".
You can use .filter for this with destructuring assignment to build a new array of wanted objects/items. Here,
if the inner arrow function returns true the item (ie object) will be kept within the new array, if it returns false it wont be added.
const arr = [{code: "sku", label: "SKU", value: "Number: 312312"},
{code: "show_more_options", label: "Show More Options", value: "New variant!"}];
const searchCode = "sku";
const res = arr.filter(({code}) => code !== searchCode);
console.log(res);

Distinct Values using Set

I have been using
<FormGroup>
<Campaign
campaignName={this.campaignName.bind(this)}
campaignOptions={[...new Set(this.props.records.map(options => options.campaign))]}/>
</FormGroup>
to get a unique array for a dropdown option, however I now need to pass an array with each object in it having a label and a value.
I have refactored the code as follows,
<FormGroup>
<Campaign
campaignName={this.campaignName.bind(this)}
campaignOptions={[...new Set(this.props.records.map(options => {
return {value: options.campaign, label: options.campaign };
}))]}/>
</FormGroup>
...but the unique element is now not working (see below with duplicates). What am I doing wrong please?
The input array looks like...
[
1. 0:{_id: "zWTiLEjqrCKdxTvze", owner: "K7xTxu7PRDCuhFZRC", company_ID: "1", campaign: "newevent", …}
2. 1:{_id: "SkZzDMAWokFFYEycp", owner: "K7xTxu7PRDCuhFZRC", company_ID: "1", campaign: "instagramlaunch", …}
3. 2:{_id: "p4pychanC5Zw8iWAQ", owner: "K7xTxu7PRDCuhFZRC", company_ID: "1", campaign: "stuffinhere", …}
… more in here including more than 1 object with instagram launch for example
]
Original approach resulted in Array of...
0:"newevent"
1:"sheffieldevent"
2:"stuffinhere"
3:”instagramlaunch"
4:”anothertest"
5:”yetanother"
now results in...
0:{value: "newevent", label: "newevent"}
1:{value: "sheffieldevent", label: "sheffieldevent"}
2:{value: "stuffinhere", label: "stuffinhere"}
3:{value: "instagramlaunch", label: "instagramlaunch"}
4:{value: "instagramlaunch", label: "instagramlaunch"}
5:{value: "instagramlaunch", label: "instagramlaunch”}
6:{value: "anothertest", label: "anothertest”}
7:{value: "yetanother", label: "yetanother"}
new Set(this.props.records.map(options => {
return {value: options.campaign, label: options.campaign };
}))
Does not work, because every new object is a unique object and Set works with all types. As result, you get all options, but not unique options.
If you have the same pattern as you want, you could render the set after collecting all values and build a new objects array out of the set.
var optionsSet = new Set(array.map(option => option.campaign)),
options = Array.from(optionsSet, v => ({ value: v, label: v }));
I resolved this using lodash...
campaignOptions={_.uniqBy(this.props.records.map((options,index) => {return { value: index, label: options.campaign }}),'label')}/>

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

JS: how to read object array with dynamic properties?

backend server returns a sessions object with an array embedded, when I log it in the console, I get this:
Object {sessions: Array[3]}
sessions: Array[3]
0: Object
data: Object
id: "22"
__proto__: Object
1: Object
data: Object
id: "23"
__proto__: Object
2: Object
data: Object
id: "48"
__proto__: Object
length: 3
__proto__: Array[0]
__proto__: Object
so, there is an extra layer of objects with dynamic property names 0,1,2...
but I need to fill in a pure array of objects, like this:
[{id: "22", data: Object}, {id: "23", data: Object}, {id: "48", data: Object}]
so, I would refer to it like sessions[0].id... sessions[1].data... etc..
how should I reformat sessions to make it work as I wish?
sessions[0].id... sessions[1].data
Yes, exactly as you said. Just add the returned object (whatever name you gave it - I'll call it returnedObject below)
console.log(returnedObject.sessions[0].id);
Or to get all sessions
console.log(returnedObject.sessions);
Or in a variable
var sessions = returnedObject.sessions;
Or all ids
var sessions = returnedObject.sessions;
sessions.forEach(session => {
console.log(session.id);
});

Categories

Resources