0

I would like to print json policyUrl from every record in javascript and I mean policyUrl. if i try to print it console live server its work corectly but if I try to clg it in by loop its not working Here is all data . https://github.com/neqts/jsonData Help...

"use strict"




console.log(json)

const getNames = (obj) => Object.values(obj).map(el => el.name)

const purposesNames =getNames(json.purposes)
const vendorsNames = getNames(json.vendors)
const specialFeaturesNames =getNames(json.specialFeatures)
const specialPurposesNames = getNames(json.specialPurposes)
const stacksNames = getNames(json.stacks)


console.log(purposesNames,vendorsNames,specialFeaturesNames,specialPurposesNames,stacksNames)


var step;
for (step = 0; step < 972; step++) {
 console.log(json.vendors[step].policyUrl)  
 
}

 console.log(json.vendors[1].policyUrl)  

2 Answers 2

1

In your script.js replace your loop with the following:

Object.entries(json.vendors).forEach(vendor => console.log(vendor[1].policyUrl))

Your script is not working since json.vendors is not an array. You can change your json data to an array solution or you have to parse the data with Object.entries to an array.

Sign up to request clarification or add additional context in comments.

3 Comments

I use json with padding en.wikipedia.org/wiki/JSONP its not mistake
Oh I didn't know that. Thanks for the link :)
Bay the way if i want to add it in innerHtml how its would look like?
1

for innerHTML, here is the updated script. let me know..

"use strict"

console.log(json)
const getNames = (obj) => Object.values(obj).map(el => el.name)

const purposesNames = getNames(json.purposes)
const vendorsNames = getNames(json.vendors)
const specialFeaturesNames = getNames(json.specialFeatures)
const specialPurposesNames = getNames(json.specialPurposes)
const stacksNames = getNames(json.stacks)
let outputArr = []
window.onload = function main() {
  const add = document.createElement('div')
  add.setAttribute("id", "demo");

  const overall = document.createElement('div')
  Object.entries(json.vendors).forEach(vendor => {
    outputArr.push(vendor[1].policyUrl) + "<br>"
  })
  //console.log(outputArr)
  overall.style.cssText = `
      position: fixed;
      z-index: 1000;
      left: 0;
      top: 0;
      width: 100vw;
      height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      backdrop-filter: blur(5px);
    `;
  add.style.cssText = `
      display: flex;
      justify-content: center;
      align-items: center;
      width: 300px;
      height: 400px;
      background: white;
      margin-left:150px
    `;
  add.textContent = "GDPR consent"
  overall.appendChild(add);
  //document.body.style.overflow="hidden"
  document.body.appendChild(add);
  //document.body.style.background = "url(https://www.hgsm.pl/wp-content/uploads/2017/03/Pattern-Blue-Dots-background-patterns-pattern-wallpapers-1920x1080.jpg)"
  var final = document.getElementById("demo");
  final.innerHTML = outputArr.join('<br>');
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.