I have 2 arrays in jade, with different length sizes I just want to validate if the id is the same so that those are selected and the others are not
// jsCategory length 6
var jsCategory = array1
// jsCategory length 3
var jsMy = array2
var html_option = '';
for (var i = 0; i < jsCategory.length; i++) {
for (var z = 0; z < jsMy.length; z++) {
if (jsCategory[i]._id == jsMy[z]._id) {
//Correct
html_option += '<option value=' + jsCategory[i]._id + ' selected>' + jsCategory[i].name + '</option>';
} else {
//Not Select err
console.log(jsCategory[i]);
}
}
}
document.write(html_option);
It's shorter to use filter and reduce
const findById = inWhere => ({ _id }) => !!inWhere.find(t => t._id === _id)
const html_option = array1
.filter(findById(array2))
.reduce((html, ({_id, name}) =>
html += `<option value=${_id} selected>${name}</option>`, ''))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.