简体   繁体   中英

Print data in javascript from json

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)

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.

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>');
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM