I have a line in the json file:

"Sotrudniki": "Петров (с 2012-05-05)\n" +"Иванов (с 2012-05-05)\n" +"Сидоров (с 2012-11-07)"

How to make a conclusion only the first line: Petrov (from 2012-05-05) , later on the bottom there will be a button to show everyone, which you can click to see the rest.

    3 answers 3

     var data = {"Sotrudniki": "Петров (с 2012-05-05)\n" + "Иванов (с 2012-05-05)\n" + "Сидоров (с 2012-11-07)"} console.log(data.Sotrudniki.split("\n")[0]) 

    In json there seems to be no operations like addition.

       console.log('"Петров (с 2012-05-05)\n" +"Иванов (с 2012-05-05)\n" +"Сидоров (с 2012-11-07)"'.replace('\n', '').split('+')[0]) 

      • Not exactly, in the console, the data from json is displayed simply in a column. There is an assumption that you need to cut it somehow before \ n, i.e. read one line. And then bring all the rest. - Fess Laeda
      • Added code cleaning \ n - Komdosh

      Perhaps the following algorithm for working with a json file will be more convenient.

      • Read file with fetch or xhr
      • Get an object or an array of objects from its contents.
      • To process the received data using javascript

       const container = document.querySelector('.container'); fetch('http://jsonplaceholder.typicode.com/users') .then(res => res.json()) .then(users => { showFirst(users, container); }) .catch(err => { throw err; }); function showFirst(users, container) { addUser(users[0], container); const showMoreButton = addShowMoreButton(container); showMoreButton.addEventListener( 'click', showAllUsers.bind( undefined, container, showMoreButton, users ) ); } function addUser(user, container) { const userBox = document.createElement('div'); userBox.innerHTML = ` <p>Name: <span>${user.name}</span></p> <p>Email: <span>${user.email}</span></p> `; container.appendChild(userBox); return userBox; } function addShowMoreButton(container) { const showMoreButton = document.createElement('button'); showMoreButton.innerText = 'Show more...'; container.appendChild(showMoreButton); return showMoreButton; } function removeElement(element) { element.parentNode.removeChild(element); } function showAllUsers(container, showMoreButton, users) { removeElement(showMoreButton); for (let i = 1; i < users.length; i++) { addUser(users[i], container); } } 
       <div class="container"></div> 

      • Interestingly, more thanks for the detailed answer! - Fess Laeda