Your code has several syntax errors (unbalanced quotes, mismatching quotes, trailing &
without quotes, …), and variables that have not been defined with var
, let
or const
. It assigns to key_for_url
, but never uses that value. It references a “slug” property, but that doesn’t exist in your input data. It assumes a certain key order in plain objects, as it uses indexOf
on Object.keys
. This is a code smell. Variable names json1
and json2
are not very descriptive.
Here is code you could use:
let filter = {
"sku Brand": "abc",
"strngth": "ALL",
"area": "",
"Country": "",
"local Brand": "",
"theme": "HideDisNameFilters"
}
let tabs = {
"nav": [{
"tabname": "tab1",
"exclude": ["area", "xyz"]
},
{
"tabname": "tab2",
"exclude": ["Country"]
}
]
}
let result = tabs.nav.map(({tabname, exclude}) =>
`<a href='${
Object.entries(filter)
.filter(([key]) => !exclude.includes(key) && key != "theme")
.map(([key, value]) => `${key}=${value}`)
.join("&")
}' target='_blank'>${tabname}</a>`
);
console.log(result);
Solution:
obj1 is an array, so the loop will be obj1.forEach and accessing the value will be prop[‘exclude’].
I have made the code a bit more short.
json1 = {
"sku Brand": "abc",
"strngth": "ALL",
"area": "",
"Country": "",
"local Brand": "",
"theme": "HideDisNameFilters"
}
json2 = {
"nav": [{
"tabname": "tab1",
"exclude": ["area", "xyz"]
},
{
"tabname": "tab2",
"exclude": ["Country"]
}
]
}
final_array = []
var obj1 = json2.nav;
obj1.forEach(function (prop) {
let str = "";
Object.keys(json1).forEach((key) => {
if (!prop['exclude'].includes(key) && key !== 'theme') {
newKey = key.split(' ').join('+');
str = str + newKey + '=' + json1[key] + "&";
}
})
var t1 = "<a href=" + "'" + str + "'" + " target = '_blank' > "+ prop['tabname'] +" < /a>"
final_array.push(t1)
});
console.log(final_array)
Am having the arrays,
Now i need to get all tab names looping through and exclude the values present in exclude.
json1 ={
"sku Brand": "abc",
"strngth": "ALL",
"area": "",
"Country": "",
"local Brand": "",
"theme": "HideDisNameFilters"
}
json2 = {
"nav": [{
"tabname": "tab1",
"exclude':["area',"xyz"]
},
{
"tabname": "tab2",
"exclude":["Country"]
}
]}
var obj1 = json2.nav;
console.log(obj1)
Object.keys(obj1).forEach(function(prop) {
var str1 = "";
var maxLength = Object.keys(json1).length-2
Object.keys(json1).forEach(key => {
var str = "";
var t1 = "";
var index = Object.keys(json1).indexOf(key);
if(key != "theme"){
if(!obj1[prop]['exclude'].includes(key)){
str = key + "="+ json1[key];
str1 +=str&
console.log("str",str, " = ",str1 )
if(maxLength == index){
var t1 = "<a href="+str1 + "target='_blank'>"+ obj1[prop]['tabname'] +"</a>"
final_array.push(t1)
}
}
}
});
});
o/p should be: (it will exclude and form the url by checking from exclude array as below)
["<a href='sku+Brand=abc&Strngth=ALL&Country=&local+Brand=&' "target='_blank'>tab1<a>,"<a href='sku+Brand=abc&Strngth=ALL&area=&local+Brand=&' "target='_blank'>tab2<a>"]
AM not getting the correct output as expected…
Your code has unbalanced quotes. Fix those first.