最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What's the best way to find the last non empty value in array - Stack Overflow

programmeradmin3浏览0评论

I have a function where I get the first non empty value in a multidimensionnal array. In my example the first non empty value is B because I through the array by thecolumns first. Now, in this function, I need to get also the last non empty value. In my example "C" is the last non empty value. Because "C" is in the furthest column. So... How I can get C in my function ?

Thank you !

function findFirstAndLastValue (timeline) {
  for (var col = 0; col < timeline[0].length; col++) {
      for (var row = 0; row < 4; row++) {
        if (timeline[row][col] != '') {
          return [row, col];
        }
      }
    }
}

function getFirstAndLastValue() {
    var projectTl = [
      ['','','A','A','',''],
      ['','B','B','','',''],
      ['','','','','C',''],
      ['','','D','D','','']
    ] 

    var position = findFirstAndLastValue(projectTl);
    console.log(position)
}

getFirstAndLastValue();

I have a function where I get the first non empty value in a multidimensionnal array. In my example the first non empty value is B because I through the array by thecolumns first. Now, in this function, I need to get also the last non empty value. In my example "C" is the last non empty value. Because "C" is in the furthest column. So... How I can get C in my function ?

Thank you !

function findFirstAndLastValue (timeline) {
  for (var col = 0; col < timeline[0].length; col++) {
      for (var row = 0; row < 4; row++) {
        if (timeline[row][col] != '') {
          return [row, col];
        }
      }
    }
}

function getFirstAndLastValue() {
    var projectTl = [
      ['','','A','A','',''],
      ['','B','B','','',''],
      ['','','','','C',''],
      ['','','D','D','','']
    ] 

    var position = findFirstAndLastValue(projectTl);
    console.log(position)
}

getFirstAndLastValue();

Share Improve this question edited Feb 2, 2018 at 18:06 user47589 asked Feb 2, 2018 at 17:56 christophebazinchristophebazin 2002 gold badges4 silver badges15 bronze badges 7
  • 2 why not try reversing the loop so you start from the end. – Jared Commented Feb 2, 2018 at 18:00
  • You cannot use return here. And you could better run backwards – mplungjan Commented Feb 2, 2018 at 18:01
  • Your code is missing a few semicolons. – user47589 Commented Feb 2, 2018 at 18:06
  • @Amy irrelevant. Semicolons are only mandatory in inline handlers and code that is to be minified. – mplungjan Commented Feb 3, 2018 at 6:28
  • @mplungjan is it irrelevant? for the sake of consistency and readability, i disagree with you – user47589 Commented Feb 3, 2018 at 19:17
 |  Show 2 more ments

4 Answers 4

Reset to default 2

Using your exact same code, but making it loop backwards instead of forwards gets you the desired result.. you could also reverse the array, or take any number of "functional" approaches.

function findFirstAndLastValue (timeline) {
  for (var col = timeline[0].length; col--;) {
      for (var row = 4; row--;) {
        if (timeline[row][col] != '') {
          return [row, col];
        }
      }
    }
}

var projectTl = [
  ['','','A','A','',''],
  ['','B','B','','',''],
  ['','','','','C',''],
  ['','','D','D','','']
] 

  var position = findFirstAndLastValue(projectTl);
  console.log(position)

You could use reduce() method and return one object as a result.

var projectTl = [
  ['', '', 'A', 'A', '', ''],
  ['', 'B', 'B', '', '', ''],
  ['', '', '', '', 'C', ''],
  ['', '', 'D', 'D', '', '']
]

const findInArr = data => {
  let temp = {
    first: null,
    last: null
  }
  return data.reduce((r, arr) => {
    arr.forEach(function(e, i) {
      if (e) {
        if (temp.last == null || i > temp.last) {
          temp.last = i
          r.last = e
        }
        if (temp.first == null || i < temp.first) {
          temp.first = i;
          r.first = e;
        }
      }
    })
    return r;
  }, Object.assign({}, temp))
}

console.log(findInArr(projectTl))

If you are looking for one liner in js, you can do it like this

// For Single arrays | get last non empty value 
let your_array = ['','A','B','','C','', ''];
let last_non_empty_value = your_array.filter(item => item).pop(-1);
console.log(last_non_empty_value);

// for Getting Last Non Empty values only from multi-dimensional arrays
let a = [
    ['', '', 'A', 'E', '', ''],
    ['', 'B', 'C', '', '', ''],
    ['', '', '', '', 'D', ''],
    ['', '', 'F', 'G', '', '']
];
let last_values = a.map(item=>item.filter(item=>item).pop());
console.log(last_values);

You need to loop your cols from the last index:

for (var col = timeline[0].length - 1; col > 0; col--)

function findFirstAndLastValue(timeline) {
 var result = {
 "first": [],
 "last": []
 };
  first: for (var col = 0; col < timeline[0].length; col++) {
    for (var row = 0; row < 4; row++) {
      if (timeline[row][col] != '') {
         result.first = [row, col];
         break first;
      }
    }
  }
  
  last: for (var col = timeline[0].length - 1; col > 0; col--) {
    for (var row = 0; row < 4; row++) {
      if (timeline[row][col] != '') {
        result.last = [row, col];
        break last;
      }
    }
  }
  
  return result;
}

function getFirstAndLastValue() {

  var projectTl = [
    ['', '', 'A', 'A', '', ''],
    ['', 'B', 'B', '', '', ''],
    ['', '', '', '', 'C', ''],
    ['', '', 'D', 'D', '', '']
  ]

  var position = findFirstAndLastValue(projectTl);
  console.log(position);
}

getFirstAndLastValue();

发布评论

评论列表(0)

  1. 暂无评论