I am working on creating an Object that consists of 3 other Objects, with one of them containing three additional Objects. My goal is to set a value for each of these Objects.
However, I am unsure about the types of Objects present as they all seem to be Object types.
I attempted to traverse through the Objects, first recognizing whether it is an Object or Array, then traversing twice and ultimately trying to set the values. Unfortunately, this led to an error being displayed in the console.
>Cannot set property 'www.google.com' of undefined
TypeError: Cannot set property 'www.google.com ' of undefined
at eval (userscript.html?name=
at Array.forEach ()
The original script involves excluding the four levels of nested Objects. Check out the code for the browser search tool here
Object.keys(iconsData).forEach(function(value1) {
if(iconsData[value1].constructor === Array){
iconsData[value1].forEach(function (value2) {
console.log('typeArray--'+iconsData[value1].constructor)
value2.host.forEach(function(host) {
iconsData.hostCustomMap[host] = value2.custom // The console shows an Error maybe here
})
})
} else if (iconsData[value1].constructor === Object) {
Object.keys(iconsData[value1]).forEach(function (value2) {
console.log('typeObject--'+iconsData[value1].constructor)
iconsData[value1][value2].forEach(function (value3) {
value3.host.forEach(function(host){
iconsData.hostCustomMap[host] = value3.custom
});
});
});
}
})
The below code contains nesting Objects within the structure.
(function () {
'use strict';
var keyword = {
beforePopup: function (popup) {
var text = window.getSelection().toString().trim();
GM_setValue('search', text);
popup(text);
},
beforeCustom: function (custom) {
var text = GM_getValue('search');
GM_setValue('search', '');
custom(text);
},
};
var iconsData = {
iconArraya: {
Arraya: [
{
name: 'Google',
image:'https://i.ibb.co/R9HMTyR/1-5.png',
host: ['www.google.com'],
popup: function (text) {
open('https://search.google.com/live?keyword=' + encodeURIComponent(text));
}
}
],
Arrayb: [
{
name: 'Bing',
image: 'https://i.ibb.co/pwkLTFc/1.png',
host: ['www.bing.com'],
popup: function (text) {
open('https://www.bing.com/live?keyword=' + encodeURIComponent(text)');
}
}
],
Arrayc: [
{
name: 'Youtube',
image:'https://i.ibb.co/FWVJ3Kf/1-2.png',
host: ['www.youtube.com'],
popup: function (text) {
open('https://www.youtube.com/live?keyword=' + encodeURIComponent(text)');
}
}
]
},
iconArrayb: [
{
name: 'open',
image:'https://i.ibb.co/fxpm6Wc/image.png',
host: [''],
popup: function (text) {
open(encodeURIComponent(text));
}
}
],
iconArrayc: [
{
name: 'copy',
image:'https://i.ibb.co/PQ5xM2R/2-1.png',
host: [''],
popup: function (text) {
document.execCommand('copy', false, null))
}
}
],
hostCustomMap: {}
}