hi I want to get the result of this function at once inside the items variable, but when I try to return items I get undefined value , can someone explain why and fix it please?
let arr= [[1,2,3,4],[5,6,7,8],['a','b','c','d']];
let colp = [0,1,3];
function selCol(arr,colp) {
let items = [];
return colp.forEach((element) => {
return arr.map((row) => {
items.push(row[element]);
return items;
});
});
}
Maybe you can return items, and in that case map is not necessary
function selCol(arr, colp) {
let items = [];
colp.forEach((element) => {
arr.forEach((row) => {
items.push(row[element]);
return items;
});
});
return items;
}
It is about the 'forEach'. forEach method returns undefined, if you want to return a value either map it or if you want to items in its mutated version, return it directly as such;
function selCol(arr, colp) {
let items = [];
colp.forEach((element) => {
return arr.map((row) => {
items.push(row[element]);
return items;
});
});
return items;
}
Related
The 1st set of code works fine and assigns non-empty objects to the var, but the 2nd code with only log the right data but will not return the data. The var just remains undefined.
Any idea on where I am going wrong? Thanks!
var filteredEmpty1 = json_data.children.filter(function(value, index, arr) {
if (value.children.length != 0) {
return value//Returns what I need
} else {
console.log("EMPTY")
};
});
json_data = filteredEmpty1;
json_data = {
"name": "RVs",
"children": json_data
};
var filteredEmpty2 = json_data.children.forEach(function(value) {
value.children.filter(function(e) {
if (e.children.length != 0) {
console.log(e)//Logs what I need to return
return(e)//Returns undefined
} else {
console.log("EMPTY")
};
});
})
Use Map instead of forEach because Map returns a value. and Filter function always returns boolean.
var filteredEmpty2 = json_data.children.map(function(value) {
return value.children.filter(function(e) {
return e.children.length != 0;
});
})
I have a function like this,
const isDisplayStaticAndConditionalSwitcher = (fieldNode, window) => {
const fieldDataSourceCode = fieldNode.getAttribute('DataSourceCode') || [];
const nodeValues = Object.values(window?.Designer?.nodes); // get the nodes values
const formDataSourceCode = nodeValues.map((o) => {
if (o.displayName === 'Form') { return o.props.code; }
}).filter((v) => v)[0];
return fieldDataSourceCode === formDataSourceCode;
};
I am getting the error, expected to return a value at the end of the arrow function error How should I resolve the issue?
Your lint rules want you to explicitly return undefined:
nodeValues.map((o) => {
if (o.displayName === "Form") {
return o.props.code;
} else {
return undefined;
}
});
The lint error is because of if condition inside map function. you need to return the same value or other in case of if condition fails.
Using map, Expectation from map function to return same length of Array.
const formDataSourceCode = nodeValues.map((o) => {
if (o.displayName === 'Form') { return o.props.code; }
// add the return in case if condition fails.
return o;
}).filter((v) => v)[0];
Hope this is helpful.
In my script below you can see what i have made. Idea is to make this filter products on page by adding values to array and check class later.
// FILTERS
var wybranyKolor = [];
var wybranyRozmiar = [];
function selectFilter(){
document.querySelectorAll(".grupa").forEach(group => {
var colorFilter = group.querySelector('.button-group-color').querySelectorAll('.button')
var sizeFilter = group.querySelector('.button-group-size').querySelectorAll('.button')
// KOLORY
colorFilter.forEach((button) => {
button.addEventListener("click", (e) =>{
colorFilter.forEach((thisColor) => {
thisColor.classList.remove("sg_active");
wybranyKolor.pop(filtr);
})
button.classList.add("sg_active");
var filtr = button.getAttribute('data-filter');
wybranyKolor.push(filtr);
showProducts();
console.log(wybranyKolor);
});
});
// ROZMIAR
sizeFilter.forEach((button) => {
button.addEventListener("click", (e) =>{
sizeFilter.forEach((thisSize) => {
thisSize.classList.remove("sg_active");
wybranyRozmiar.pop(filtr);
})
button.classList.add("sg_active");
var filtr = button.getAttribute('data-filter');
wybranyRozmiar.push(filtr);
showProducts();
console.log(wybranyRozmiar);
});
});
});
}
Here i get stuck, any ideas how to console.log values from array same as class in divs ?
Uncaught TypeError: boxPro.forEach is not a function
function showProducts(){
document.querySelectorAll('.grid').forEach(gridDiv => {
var boxPro = gridDiv.querySelector('.box_pro');
boxPro.forEach((elementDiv) => {
if(elementDiv.classList.contains(wybranyKolor)){
console.log('MAM ' + wybranyKolor);
}
});
});
}
You're trying to call an array prototype method on a primitive value. The querySelector() only selects the first element it finds, so you're not able to iterate over it. If you really need the forEach method, you may be able to do a querySelectorAll() and have it return an array with 1 element.
I'm modifying the property of an object inside an array in the following way:
const newPanoramas = state.panoramas.map(panorama => {
if (state.panorama.id === panorama.id) {
panorama.thumbnail = thumbnail
}
})
I know I have to return something---but I'm not very sure what ...
You forgot the most important part: to return the mapped object. Should be :
const newPanoramas = state.panoramas.map(panorama => {
if (state.panorama.id === panorama.id) {
panorama.thumbnail = thumbnail
}
return panorama;
});
What you have to return is the object you modified
const newPanoramas = state.panoramas.map(panorama => {
if (state.panorama.id === panorama.id) {
panorama.thumbnail = thumbnail
}
return panorama;
})
That object will be returned an stored in newPanorama.
I have code that runs promises
phrasesUpdate = (): ng.IPromise<any> => {
var self = this;
var promises = [];
angular.forEach(self.phrases, (ph, key) => {
if (ph.statusId == Status.Dirty) {
ph.createdDate
? promises.push(self.phraseUpdateSubmit(ph, key))
: promises.push(self.phraseAddSubmit(ph, key));
}
});
return self.$q.all(promises);
};
This code works, however I would like to run this code:
self.phrases.forEach(function (phrase, index) { phrase.index = index; })
inside of this function before returning.
Is there some way that I can fit this into the function?
Just add then:
return self.$q.all(promises).then(result => {
// Your last thing goes here
return result;
});
Re your edited question: I believe this is how you integrate that line into the code:
return self.$q.all(promises).then(result => {
self.phrases.forEach(function (phrase, index) { phrase.index = index; });
return result;
});
(I'm assuming here that the phraseUpdateSubmit and phraseAddSubmit modify self.phrases, which is why we're waiting to do this until after the promises they return complete.)