Distinct Values using Set - javascript

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')}/>

Related

How can I extract and pair the values of an array based object

I'm trying to create a String based upon an object consisting of several key-value pairs.
Example:
[{ name: 'cookie1', value: 'false' },
{ name: 'cookie2', value: '123' },
{ name: 'cookie3',value: 'abc'}]
What I'm trying to achieve (string):
cookie1: false, cookie2: 123, cookie3: abc
I've tried to extract just the val using map like this (played around moving around values):
var output = cookies.map(d => {
return {
"name": d.name,
"value": d.value,
}
})
One way to do this is to map the array of objects into name: value strings and then join them with , :
const data = [{ name: 'cookie1', value: 'false' },
{ name: 'cookie2', value: '123' },
{ name: 'cookie3',value: 'abc'}]
const result = data.map(({ name, value }) => `${name}: ${value}`).join(', ')
console.log(result)

Can't push array item to array data property - Vue Js

I a getting data from the laravel using this response :
$unserialize = unserialize($import->field_names);
return response()->json( $unserialize, 200 ) ;
Now on the Vue JS I can console the response using this :
console.log(response);
and It's showing the data in array (Check the red arrow mark):
Now, I have a options empty array property like this:
options : []
I want to push object data to this array with a key value and text. This key's value will be that response data single item. So, To do this, I am using this:
response.data.forEach((item, index) => {
this.options.push({
value: item,
text: index
});
});
but If I console
console.log(this.options);
I can not see the array of object to this options propery. I can see this:
can you tell me why? I want this options should store the item like this:
options: [
{ value: null, text: 'Please select some item' },
{ value: 'a', text: 'This is First option' },
{ value: 'b', text: 'Default Selected Option' },
{ value: 'c', text: 'This is another option' },
{ value: 'd', text: 'This one is disabled', disabled: true },
]
Update:
Console.log(response);
I think that the problem could be in this keyword
you can modify your code in this way
this.options = [...this.options,
...response.data.map((item, index) =>
({
value: item,
text: index
})
)]

Map value from array to different array type

Im trying to add to an array of object list of objects with key value (typescript)
Hardcoded value
const Runtimes: QuickPickItem[] = [{
label: '$(package-dependents-16) Merge Branch',
description: '$(git-commit) 1 commit',
detail: '$(diff-added) 3 $(diff-modified) 2'
},
{
label: '$(package-dependents-16) commit Branch',
description: '$(git-commit) 2 commit',
detail: '$(diff-added) 3 $(diff-modified) 2'
},
];
When I do it like this it works!
const pick = await input.showQuickPick({
title,
step: 1,
totalSteps: 2,
placeholder: 'Choose runtime',
items: Runtimes,
activeItem: typeof state.resourceGroup !== 'string' ? state.resourceGroup : undefined,
})
Now I’ve array of objects which I want to put inside the Runtime
res.Runtime.data =
Array(2) [Object, Object]
Object {id: "14", name: "event1”, description: "delete order event", …}
Object {id: "89", name: "event2", description: "create order event", …}
I want to use the name and description.
I want that this res.Runtime.data = Runtimes (name & description)
I tried to create an interface
interface myObj {
name: string;
description: string;
}
But still I see one entry, any idea?
At the end runtime should look like
const Runtimes: QuickPickItem[] = [{
label: 'event1',
description: 'delete order event'
},
{
label: 'event2',
description: 'create order event'
},
];
I need it to be array of object with label and description
currently I've tried with
const Runtimes: [] = res.Runtime.data .map(((item: { name: string; description: string }) => (item.name, item.description));
But I get just the name and not an object with name and description

How to count object's number in array?

There is a function which create the same objects (with different values of ID, task and point) and push it to array Box. And i want to get each number of object, and show it to user on page. 1,2,3,4,5,6.... How to do it?
box: [{
id: shortid.generate(),
task: action.task,
point: false
}]
I believe you want to add sequence number to each of your object:
const result = [{
id: '2s3wf4',
task: 'task1',
point: false
}].map((x, i) => ({
...x,
serialNumber: i + 1
}));
console.log(result)

AngularJS : How to concat two arrays?

I have following arrays with values (i am generating the values on go)
$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];
I want to concatenate these two objects to get the following result
[{Name:''},{ Key: '', Value: '' }]
Plunker link , somehow that's not working either
when I click on the add row button it will add another row for key and value text boxes only not for name, I can add n no of rows and when I click on Submit it should show the kev value pair as
[{Name:''},{ Key: '', Value: '' },{ Key: '', Value: '' },{ Key: '', Value: '' }.....so on]
Thanks
Not sure why you want to build an array of mismatched objects. That seems to me to be asking for trouble. I would suggest possibly doing the following:
$scope.objects = [{Name: '', Elements: []}];
Then you can easily manage multiple objects who have elements:
(I use underscore http://underscorejs.org/)
$scope.addElementToObject = function(objName, element){
_.where($scope.mergedArray, {Name: objName}).Elements.push(element);
};
Then you can add to the list of elements for that object without having to eval the object in the elements array on each use.
If you still want/need to merge arrays of mismatched objects, it would be the following:
$scope.objectName = [{ Name: '' }];
$scope.propertiesElement = [{ Key: '', Value: '' }];
$scope.mergedArray = $scope.objectName.contact($scope.propertiesElement);
$scope.addElement = function(element){
$scope.mergedArray.push(element);
};
Then, in your click event code:
$scope.addElement({ Key: 'someKey', Value: 'Some Value' });
I hope this helps.

Categories

Resources