How to store all array value to one array? - javascript

Title is a bit 'misleading, basically I'm trying to replicate an array like this:
'columns': [
{ id: 1, name: 'Operator1' },
{ id: 2, name: 'Operator2' }
]
in one variable:
var myMap = {id: ['0', '1'], name:['Operator1', 'Operator2']}
alert(myMap.id[0]);
The problem's that columns doesn't get the value of myMap, maybe I'm wrong with the array struct? See this code:
Essentially the result that's a I want achieve is this:
'multiColAgendaWeek': {
'type': 'multiColAgenda',
'duration': { weeks: 1 },
'columns': myMap
}
If instead I pass myMap like myMap.id or myMap.Name I get:
undefined
on my fullcalendar.
What am I doing wrong?
PS: I'm using this class extension: https://github.com/mherrmann/fullcalendar-columns/
Image explain:
If I use the myMap array as columns: myMap

Simple map function to convert it.
var myMap = {id: ['0', '1'], name:['Operator1', 'Operator2']};
var updated = myMap.id.map(function (val, ind) {
return { "id" : val, "name" : myMap.name[ind] };
});
console.log(updated);
Still would be better to do it on wherever the data is being generated.

Related

How can I get sibling value in JS object?

How can I get 'Pivot Grid', when I have 7? Is there method or can you help me with function?
WIDGET_TYPES = {
PIVOT_GRID: {
LABEL: 'Pivot Grid',
ID: 7,
},
DATA_GRID: {
LABEL: 'Grid',
ID: 4,
},
};
you can use Object.values and find
Object.values - will convert your object to array wiht next structure:
[{LABEL: 'Pivot Grid',ID: 7,}, ...]
after conversion you can apply find to new generated array.
In the find method you will pass your id(7) what need to find
then we need to take label from found element of array
const WIDGET_TYPES = {
PIVOT_GRID: {
LABEL: 'Pivot Grid',
ID: 7,
},
DATA_GRID: {
LABEL: 'Grid',
ID: 4,
},
};
const ID = 7;
const foundLabel = Object.values(WIDGET_TYPES).find(i => i.ID === ID)?.LABEL;
console.log('foundLabel: ', foundLabel)

Test array of objects for values equal to values from another array in Chai

I'm totally new to Chai, and this is probably really simple, but I can't find any reference to my case in the official docs. I'm trying to verify that an array with two objects has a property value exactly matching values in another array.
Consider the following:
test('Array includes ids', function() {
const array1 = [
{
type: 'type1',
id: '1'
},
{
type: 'type2',
id: '2'
},
]
const array2 = ['1', '2']
chai.expect(array1).to.have.deep.members(array2) // fails
})
I'd like to verify that objects in array1 has an id property with one of the values found in array2. I know I could map array1 as array1.map(p => p.id) and compare that with array2, but that seems to go against testing as a practice. After all, I don't want to tamper too much with my target to make it conform to my current level of knowledge.
Is there a better way to test this? (Needless to say, my example code above does not work as desired.)
Just transform your array1 with .map() method to have your kind of array with only ids in it just like array2,
test('Array includes ids', function() {
const array1 = [
{
type: 'type1',
id: '1'
},
{
type: 'type2',
id: '2'
},
]
const arr1 = array1.map(e=>e.id);//transform with .map() -> ['1', '2']
const array2 = ['1', '2']
chai.expect(arr1).to.have.deep.members(array2) // pass
})

json object from javascript nested array

I'm using a nested array with the following structure:
arr[0]["id"] = "example0";
arr[0]["name"] = "name0";
arr[1]["id"] = "example1";
arr[1]["name"] = "name1";
arr[2]["id"] = "example2";
arr[2]["name"] = "name2";
now I'm trying to get a nested Json Object from this array
arr{
{
id: example0,
name: name00,
},
{
id: example1,
name: name01,
},
{
id: example2,
name: name02,
}
}
I tought it would work with JSON.stringify(arr); but it doesen't :(
I would be really happy for a solution.
Thank you!
If you are starting out with an array that looks like this, where each subarray's first element is the id and the second element is the name:
const array = [["example0", "name00"], ["example1", "name01"], ["example2", "name02"]]
You first need to map it to an array of Objects.
const arrayOfObjects = array.map((el) => ({
id: el[0],
name: el[1]
}))
Then you can call JSON.stringify(arrayOfObjects) to get the JSON.
You need to make a valid array:
arr = [
{
id: 'example0',
name: 'name00',
},
{
id: 'example1',
name: 'name01',
},
{
id: 'example2',
name: 'name02',
}
];
console.log(JSON.stringify(arr));
Note that I am assigning the array to a variable here. Also, I use [] to create an array where your original code had {}.

How can I filter only a particular data from an array of objects

I just need to extract a specific data from this. For eg: mobile no:
This is just a result from the console:
.
What code should I write on my JS.
If its an object, you can just access its properties by calling its name after the dot (eg myObject.mobile) or by bracket syntax: myObject['mobile']
All you need to know: https://www.w3schools.com/js/js_properties.asp
If it's a collection, just use forEach or map function (or any other available method) to iterate over it and then access property as shown above.
You can use the library LoDash. Add the library to your project and use .pick method. If your data is an Array, then use .map method
var data = {
var1: 1,
var2: 2,
var3: 3
};
console.log(data);
var mappedData = _.pick(data, 'var2' );
console.log(mappedData);
If your data is an object,
var data = { entryId: 'ABC', mobile: '0123456789' }
console.log(data.entryId) // 'ABC'
console.log(data.mobile) // '0123456789'
If your data is an array of objects,
var data = [
{ entryId: 'ABC', mobile: '0123456789' },
{ entryId: 'BBB', mobile: '1111111111' },
{ entryId: 'CCC', mobile: '2222222222' }
];
var dataWithMobileNumbersOnly = data.map((obj) => {
return { mobile: obj.mobile };
});
console.log(dataWithMobileNumbersOnly) // [{mobile: '0123456789'}, {mobile: '1111111111'}, {mobile: '2222222222'}]
For more information about map() method, visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

JS – How to build a dynamic nested object of arrays with objects etc. from string

This is a nice evening project, but actually i'm stuck with some headache.
All I need is a function like this example:
result = set("itemCategories[0].items[0].name", "Test")
which should return:
{ itemCategories: [
{
items: [ {name: "Test"} ]
}
}]
...and in case of the given attribute "itemCategories[1].items[2].name" this result:
{ itemCategories: [
null,
{
items: [
null,
null,
{name: "Test"}
]
}
}]
Use lodash#set:
result = lodash.set({}, "itemCategories[0].items[0].name", "Test")
If you are asking about the vanilla JavaScript Set method then you could do this.
/* this is what you are trying to get.
{ itemCategories: [
{
items: [ {name: "Test"} ]
}
}]
*/
var mySet = new Set(); // your set object.
Create your data (number, text, string, object, array, null).
ver data1 = 365;
ver data2 = 'Dragonfly';
ver data3 = {name: 'Bobby', age: 20000, job: 'dj'};
Then you just add to that set using its add method.
mySet.add(data1);
mySet.add(data2);
mySet.add(data3);
So to get what you are looking for you would write this.
var itms = {items: [{name: 'test'}]};
mySet.add(itms);
The good thing about set is that is like an array. So you can use forEach.
mySet.forEach( function(val){
console.log(val); // gets all your data.
});
You can even check if a value is in your data using the has method.
mySet.has(365); // true
mySet.has(36500000); as false
JavaScript Set

Categories

Resources