fetch
?const request = (resource, init = {}) => fetch(resource, init)
.then(response => {
if (response.status === 401) {
// redirect
return Promise.reject(/* cause */);
}
return response;
});
const data = [
{ name: 'Sasha', apple: 8, banana: 6, cherry: 9 },
{ name: 'Artur', apple: 4, banana: 2, cherry: 1 },
{ name: 'Sasha', apple: 5, banana: 7, cherry: 3 },
{ name: 'Bogdan', apple: 6, banana: 11, cherry: 8 },
{ name: 'Artur', apple: 8, banana: 6, cherry: 9 },
];
const collectBy = (collection, property) =>
collection.reduce((accumulator, entry) => {
const { [property]: key, ...data } = entry;
if (!accumulator.hasOwnProperty(key)) {
accumulator[key] = data;
} else {
for (const [property, value] of Object.entries(data)) {
accumulator[key][property] += value;
}
}
return accumulator;
}, {});
const collectedData = collectBy(data, 'name');
console.log(collectedData);
/*
{
Sasha: { apple: 13, banana: 13, cherry: 12 },
Artur: { apple: 12, banana: 8, cherry: 10 },
Bogdan: { apple: 6, banana: 11, cherry: 8 }
}
*/
JSON.parse(response);
const data = JSON.parse(`{"sentences":[{"trans":"Привет мир","orig":"Hello World","backend":1}],"src":"en","spell":{}}`);
const words = data.sentences.map(sentence => sentence.trans);
const expression = /(.+)?#/;
$('a').each(function () {
if (expression.test($(this).attr('href'))) {
$(this).click(...);
}
});
localStg
? А сам JSON прекрасно парсится.const json = `[
{
"street": {
"id": 0,
"name": "string",
"type": {
"id": 0,
"name": "string"
},
"city": {
"id": 0,
"name": "string",
"type": {
"id": 0,
"name": "string"
}
}
},
"house": "string",
"id": 0
}
]`;
JSON.parse(json);
case ADD_CATEGORY:
addCategory(
- state,
+ newState,
action.category
);
break;
let newState = { ...state };
. const fs = require('fs');
const VAR_NAME = require('./JSON_NAME.json'); // Чтение
VAR_NAME.name = 'John Doe'; // Модификация
fs.writeFileSync(`${__dirname}/JSON_NAME.json`, JSON.stringify(VAR_NAME)); // Запись