简体   繁体   English

使用地图迭代对象数组,直到倒数第二个对象

[英]Iterate over array of object using map till the second last object

I am having an array of objects where all objects have the same keys except the last object. 我有一个对象数组,其中除最后一个对象外,所有对象都具有相同的键。 Think like array have values and to denote all these values as a whole I have a key I am pushing the key at last along with the values in the array. 认为像数组一样具有值,并总体上表示所有这些值,我有一个键,最后将键与数组中的值一起按下。

homeTask is a list of object which is the values and homeTaskKey is the key to represent the homeTask homeTask是对象列表,是值,而homeTaskKey是表示homeTask的键

        res.data.resultSet.homeTask.forEach(element => {
                      var singleEvent={
                        task:'',
                        taskDuration:'',
                        status:'',
                      };

                      singleEvent.task=element.task;
                      singleEvent.taskDuration=element.taskDuration;
                      singleEvent.status=element.status;

                      newEvents.push(singleEvent);


                    });
                    newEvents.push(res.data.resultSet.homeTaskKey);


                   addEvent(newEvents);
              }

addEvent is props method of parent component where I am setting the array to state variable name as events which is array type. addEvent是父组件的props方法,其中我将数组设置为状态变量名作为数组类型的events

When I iterate over events using map I want to skip the last object since it does not have keys like task, taskDuration and status. 当我使用map遍历events我想跳过最后一个对象,因为它没有像task,taskDuration和status这样的键。 Therefore it won't give any problem when I fetch those values. 因此,当我获取这些值时不会有任何问题。

You could still use map , but simply pop the last element off once the map completes. 您仍然可以使用map ,但是只需在地图完成后弹出最后一个元素。 For example: 例如:

const newEvents = homeTask.map(({ task, taskDuration, status }) => ({
  task, taskDuration, status
}))
newEvents.pop()
addEvent(newEvents)

Or just replace the last item with your taskkey, as you know the last item will be junk: 或者只是将最后一项替换为您的任务键,因为您知道最后一项将是垃圾:

newEvents[newEvents.length - 1] = res.data.resultSet.homeTaskKey

Or just slice the array prior to mapping, and then push the taskKey at the end like you were doing. 或者只是在映射之前对数组进行slice ,然后像执行操作一样将taskKey推到最后。 slice is safe to perform on your prop, as it shallow copies. 切片很浅,因此可以安全地在您的道具上执行。


Or most importantly, ask yourself why you have this entirely weird data structure that has the key appended on the end. 或最重要的是,问问自己为什么拥有一个完全奇怪的数据结构,并在其末尾附加了密钥。 Perhaps rethink your data and not create this problem for yourself in the first place. 也许重新考虑您的数据,而不是首先为您自己创建此问题。

events.slice(0, events.length-1).map(<function>);

this will ignore the last element and all n-1 entries will be fed to map 这将忽略最后一个元素,所有n-1个条目都将被馈送到地图

UPDATE : 更新

the array name is events not event therefore it should be events.length 数组名称是事件而非事件,因此应为events.length

res.data.resultSet.homeTask.forEach((element,index) => {})

函数的第二个参数是索引,可以通过将其与数组的总长度进行比较来使用它来标识倒数第二个元素。

hmm you can try with this 嗯,你可以试试这个

res.data.resultSet.homeTask.forEach(element => {
    if(!element.task)
        return false;
    ...bla bla bla
}

The map() method creates a new array with the results of calling a function for every array element. map()方法创建一个新数组,并为每个数组元素调用一个函数。

So it creates an array of same length from source array. 因此,它从源数组创建了一个相同长度的数组。

What you need is filter() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter 您需要的是filter() https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Something like this; 像这样的东西;

const tasks = res.data.resultSet.homeTask.filter((element) => {
  const { task, taskDuration, status } = element;
  return task && taskDuration && status; 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM