Javascript 如果我们有属性,如何反转数组?

Javascript 如果我们有属性,如何反转数组?,javascript,Javascript,我有一个数组dataAllDate,它可能有或没有dataAllDate。如果有,则需要反转dataAllDate顺序 试过这个 if(dataAllDate['positiveIncrease']) { console.lgo("hello"); dataAllDate.reverse(); } 基本上我收到的是: var dataAllDate = JSON.parse("["+dataAllDateToJason+"]"); 但是dataAllDate并不总是相同的,所以我

我有一个数组
dataAllDate
,它可能有或没有
dataAllDate。如果有,则需要反转
dataAllDate
顺序

试过这个

if(dataAllDate['positiveIncrease']) {
   console.lgo("hello");
   dataAllDate.reverse();
}
基本上我收到的是:

var dataAllDate = JSON.parse("["+dataAllDateToJason+"]");
但是
dataAllDate
并不总是相同的,所以我需要检查该键,如果它相同,我需要反转数组

更新

以下是数据:

allDates: Array(65)
0: {date: "2/24/20", ricoverati_con_sintomi: 0, terapia_intensiva: 0, totale_ospedalizzati: 0, isolamento_domiciliare: 0, …}
1: {date: "2/25/20", ricoverati_con_sintomi: 0, terapia_intensiva: 0, totale_ospedalizzati: 0, isolamento_domiciliare: 0, …}

您可以尝试以下方法:

if(hasPositiveIncrease(dataAllDate)){
  dataAllDate.reverse()
}
您可以这样定义
hasPositiveIncrease

const hasPositiveIncrease = (dataAllDate) => 
  dataAllDate.find((item) => item.hasPositiveIncrease);
这将返回具有正增量的第一个元素

工作片段已附加

const dataAllDate=[
{
日期:“2/24/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
},
{
日期:“2/25/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
},
{
日期:“2/26/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
},
{
日期:“2/27/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
有积极的增长:1
},
];
const hasPositiveIncrease=(dataAllDate)=>
dataAllDate.find((项)=>item.hasPositiveIncrease);
如果(有正增量(dataAllDate)){
console.log(dataAllDate.reverse())

}
您可以尝试以下方法:

if(hasPositiveIncrease(dataAllDate)){
  dataAllDate.reverse()
}
您可以这样定义
hasPositiveIncrease

const hasPositiveIncrease = (dataAllDate) => 
  dataAllDate.find((item) => item.hasPositiveIncrease);
这将返回具有正增量的第一个元素

工作片段已附加

const dataAllDate=[
{
日期:“2/24/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
},
{
日期:“2/25/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
},
{
日期:“2/26/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
},
{
日期:“2/27/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
有积极的增长:1
},
];
const hasPositiveIncrease=(dataAllDate)=>
dataAllDate.find((项)=>item.hasPositiveIncrease);
如果(有正增量(dataAllDate)){
console.log(dataAllDate.reverse())

}
我会使用
Array.some()

const dataAllDate=[{
日期:“2/24/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
积极的增加:是的,
},
{ 
日期:“2/25/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0
}];
const positiveIncreaseExists=dataAllDate.some(item=>item.positiveIncrease);
如果(正增量存在){
dataAllDate.reverse();
}

console.log(dataAllDate)我会使用
Array.some()

const dataAllDate=[{
日期:“2/24/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0,
积极的增加:是的,
},
{ 
日期:“2/25/20”,
ricoverati_con_sintomi:0,
特拉比亚:0,
总计:0,
isolamento_住所:0
}];
const positiveIncreaseExists=dataAllDate.some(item=>item.positiveIncrease);
如果(正增量存在){
dataAllDate.reverse();
}

console.log(dataAllDate)
你的
dataAllDate
是什么样子的?你的意思是你有一个对象数组
dataAllDate
?我不明白
var dataAllDate=JSON.parse(“[”+dataAllDateToJason+“])
可以将
positiveIncrease
属性添加到数组中。如果它是一个数组,而您没有添加名为
positiveIncrease
的索引,则表明您错误地访问了数组,您需要访问index@CodeManiac用数据更新了问题您的
dataAllDate
看起来如何?您的意思是您有一个对象数组
dataAllDate
?我无法理解
var dataAllDate=JSON.parse(“[”+dataAllDateToJason+“])
可以将
positiveIncrease
属性添加到数组中。如果它是一个数组,而您没有添加名为
positiveIncrease
的索引,则需要访问该数组index@CodeManiac用数据更新了问题无需
。查找
-如果您只需要布尔值,然后使用
。这意味着它将遍历整个数组。而
.find
将返回它找到的第一个满足条件的元素。虽然,我不能说两者之间的性能差异,但我使用的find远远不止一些。这两个…工作相同。“至少一个元素”表示它在第一次匹配后返回。为什么在找到匹配项后它会继续遍历整个数组?唯一的区别是
.some
返回一个布尔值,而
.find
返回元素。实际上
.some()
.find()
的工作原理相同。如果找到匹配的值,它会立即返回true。不需要
。查找
-如果只需要一个布尔值,请使用
的一些问题是它至少检查一个元素。这意味着它将遍历整个数组。而
.find
将返回它找到的第一个满足条件的元素。虽然,我不能说两者之间的性能差异,但我使用的find远远不止一些。这两个…工作相同。“至少一个元素”表示它在第一次匹配后返回。为什么还要继续