Вы можете получить его с помощью getFullyear ()
https://www.w3schools.com/jsref/jsref_getfulear.asp
const archives = {
"data": [{
"id": 1,
"type": "archive",
"attributes": {
"name": "Employee Engagement Summary",
"description": "Report description goes here",
"creation_date": "March 21, 2018",
"date_range": "03/01/2018-05/15/2018",
"data_sets": "Facility A, Nursing Department"
}
},
{
"id": 1,
"type": "archive",
"attributes": {
"name": "Sample Survey 1",
"description": "Report description goes here",
"creation_date": "March 21, 2017",
"date_range": "03/01/2017-05/15/2017",
"data_sets": "Facility A, Nursing Department"
}
},
{
"id": 1,
"type": "archive",
"attributes": {
"name": "Sample Survey 2",
"description": "Report description goes here",
"creation_date": "March 21, 2016",
"date_range": "03/01/2016-05/15/2016",
"data_sets": "Facility A, Nursing Department"
}
},
{
"id": 1,
"type": "archive",
"attributes": {
"name": "Sample Survey 3",
"description": "Report description goes here",
"creation_date": "March 21, 2015",
"date_range": "03/01/2015-05/15/2015",
"data_sets": "Facility A, Nursing Department"
}
}
,
{
"id": 1,
"type": "archive",
"attributes": {
"name": "Sample Survey 3",
"description": "Report description goes here",
"creation_date": "March 21, 2014",
"date_range": "03/01/2014-05/15/2014",
"data_sets": "Facility A, Nursing Department"
}
},
{
"id": 1,
"type": "archive",
"attributes": {
"name": "Sample Survey 3",
"description": "Report description goes here",
"creation_date": "March 21, 2013",
"date_range": "03/01/2013-05/15/2013",
"data_sets": "Facility A, Nursing Department"
}
},
{
"id": 1,
"type": "archive",
"attributes": {
"name": "Sample Survey 3",
"description": "Report description goes here",
"creation_date": "March 21, 2012",
"date_range": "03/01/2012-05/15/2012",
"data_sets": "Facility A, Nursing Department"
}
}
]
};
let arr=archives.data.map(a=>new Date(a.attributes.creation_date).getFullYear());
console.log(arr);
Возможно, более читаемый способ решить эту проблему - использовать Array#find
следующим образом:
const arr = [
{name: 'first', amount: 2},
{name: 'second', amount: 1},
{name: 'third', amount: 1}
];
const obj = {name: 'second', amount: 3};
const element = arr.find(el => el.name === obj.name);
if (element) {
element.amount += obj.amount;
} else {
arr.push(obj);
}
console.log(arr);
Вы можете использовать массив findIndex
и проверить, существует ли объект, имя которого совпадает с именем объекта. findIndex
return -1
результат не найден. В этом случае нажмите obj
в массиве, иначе обновите значение суммы
const arr = [{
name: 'first',
amount: 2
},
{
name: 'second',
amount: 1
},
{
name: 'third',
amount: 1
}
]
const obj = {
name: 'second',
amount: 3
}
let ifKeyExist = arr.findIndex((item) => {
return item.name === obj.name
})
if (ifKeyExist === -1) {
arr.push(obj)
} else {
arr[ifKeyExist].amount += obj.amount
}
console.log(arr)
Вам нужна другая переменная found
и установите ее на true
, если набор данных найден. Тогда не помещайте фактический набор данных в массив.
По сути, вам необходимо посетить все элементы и в конце решить, добавите ли вы объект в массив.
С вашим кодом вы выполняете хотя бы одно действие, либо обновляете фактический конец набора данных, который работает, только если требуемый name
находится в первом элементе, или вы помещаете объект в массив. Это происходит до тех пор, пока не будет найден требуемый набор данных или больше не будет доступных элементов.
const arr = [{ name: 'first', amount: 2 }, { name: 'second', amount: 1 }, { name: 'third', amount: 1 }];
const obj = { name: 'second', amount: 3 };
var found = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i].name === obj.name) {
arr[i].amount += obj.amount;
found = true;
break;
}
}
if (!found) {
arr.push(obj);
}
console.log(arr);
.as-console-wrapper { max-height: 100% !important; top: 0; }