从数组中获取数值,该数组也包含null

时间:2018-05-02 07:40:46

标签: javascript

我在一个有3个数组的对象中申请每个循环;

this.chart.ref.series.forEach(element=> {
   ...
});

在元素中,每次出现一个系列对象。 如果我查看element.options.data,我会得到一个这样的数组。

[140,290,null,null,null,null,null,null]

我的其余数组与我在for-each元素中的相同类型相同,现在我的要求是添加每个数组的最后值和起始值(非空)并将其存储为两个单独的变量,例如startingAccountValuesendingAccountValues

例如,我喜欢的三个数组

[140,290,null,null,null,null,null,null],
[110,130,120,140,null,null,null,null],
[270,390,230,null,null,null,null,null]

结果我想要像

这样的东西

startingAccountValues = 520(140 + 110 + 270) endingAccountValues = 660(290 + 140 + 230)

6 个答案:

答案 0 :(得分:0)

将数组映射到所需的内部属性,过滤掉s = Search().sort( { "_geo_distance" : { "location" : {"lat": 40, "lon": -70}, "order" : "asc", "unit" : "km", "distance_type" : "arc" } } ) 值,然后将每个数组的第一个和最后一个值添加到结果中。

null

答案 1 :(得分:0)

您可以在代码中使用let startingAccountValues = 0; let endingAccountValues = 0; this.chart.ref.series .map(x => x.options.data) .filter(x => x !== null) .map(x => { startingAccountValues += x[0]; endingAccountValues += x[x.length - 1]; }) 作为indexOf(null)-1的总和,如下面的代码所示。不要将此视为完整代码,因为数组endingAccountValues具有不同的数据结构。您需要根据series的数据结构使用此代码。核心逻辑与series

中的相同



series.forEach




答案 2 :(得分:0)

您可以使用forEach& filterfilter获取没有null的数组。然后使用array[0]获取第一个元素,使用array[array.length-1]获取最后一个元素



//Put all the arrys in an array
var arryArrys = [
  [140, 290, null, null, null, null, null, null],
  [110, 130, 120, 140, null, null, null, null],
  [270, 390, 230, null, null, null, null, null]
]

var startVal = 0;
var endVal = 0;
/iterate using forEach
arryArrys.forEach(function(item) {
  // filter elements which are not null
  let intArry = item.filter(function(el) {
    return el !== null

  });
  // add the value to the initial variables
  startVal += intArry[0];
  endVal += intArry[intArry.length - 1];


});
console.log(startVal, endVal)




答案 3 :(得分:0)

试试这个:

function getAccountValues(array) {

  let startingAccountValues = 0,
    endingAccountValues = 0;

  array.forEach(arr => {
    let filteredArr = arr.filter(e => {
      return e !== null;
    });

    startingAccountValues += filteredArr[0];
    endingAccountValues += filteredArr[filteredArr.length - 1];
  })

  return {
    startingAccountValues: startingAccountValues,
    endingAccountValues: endingAccountValues
  }
}

var workArray = [
  [140, 290, null, null, null, null, null, null],
  [110, 130, 120, 140, null, null, null, null],
  [270, 390, 230, null, null, null, null, null]
];

console.log(getAccountValues(workArray));

作为奖励,您可以编辑workArray并使其动态化,而不仅限于3个阵列!

答案 4 :(得分:0)

我想使用lodash

keywords <- c("Toyota", "Prius", "BMW", "M3")
documents <- c("New Toyota Prius for sale, the Toyota Prius is in good condition","BMW M3 that drives like a Toyota but is a BMW")
keywords <- paste0("\\b", keywords, "\\b")
res <- sapply(keywords, function(x) grepl(x, documents))
rowSums(res)

[1] 2 3

答案 5 :(得分:-1)

这应该有效:)

let barButton = UIButton()
    barButton.frame = CGRect(x:0, y:0, width:30, height:30)
    barButton.setTitle(“BarButtonTest”, for: .normal)
    barButton.backgroundColor = UIColor.yellow
    barButton.layer.cornerRadius = 5.0
    barButton.addTarget(self, action: #selector(didTapBarButton(_:)), for: .touchUpInside)

    let rightBarButton = UIBarButtonItem(customView: barButton)
    self.navigationItem.rightBarButtonItem = rightBarButton


If above answer not solve your problem then instead of using navigation bar use custom view as Navigation Bar using autolayout and take button inside it, set your title accordingly.
let arr = [110,130,120,140,null,null,null,null,666]

const appDiv = document.getElementById('app');

arr = arr.filter((item) => item !== null );

appDiv.innerHTML = arr;


appDiv.innerHTML += `<h1>First:${arr[0]}</h1>`;


appDiv.innerHTML += `<h1>Last:${arr[arr.length - 1]}</h1>`;