简体   繁体   English

如何将对象插入到对象列表的开头,当它未移位时不是函数错误?

[英]how to insert object into beginning of object list, when it has unshift is not a function error?

How to insert object element in object which is list of object ? 如何在对象列表中的对象中插入对象元素?

I have this tiny piece of code in ReactJS: 我在ReactJS中有这小段代码:

var allMessages = this.state.data;
var newMessages = allMessages.unshift([data]); // i suppose it should be unshift if it was an array, but it is an object o_O
this.setState({ data: newMessages });

Where: (chrome console output) 其中:(Chrome控制台输出)

> data
Object {id: "12", text: "1234124", date: "2016-04-28 20:00:07", sender: "user", type: "receiver"}
> allMessages
[Array[1], Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]
> typeof allMessages
"object"

How to insert data into beginning of allMessages ? 如何将data插入allMessages开头?

I have seen this reply How can I add new array elements at the beginning of an array in JavaScript? 我已经看到了此回复如何在JavaScript中的数组开头添加新的数组元素? and it is another case because my allMessages variable is an object not an Array. 这是另一种情况,因为我的allMessages变量是一个object而不是数组。

I got unshift is not a function when i try to use unshift 当我尝试使用unshift时,我得到unshift is not a function

I can think of this work around; 我可以考虑解决这个问题;

var tempMessage= {};
tempMessage.data = data;
for(var key in allMessages){
     tempMessage[key] = allMessages[key];
}

Than just set it back 比把它放回去

allMessages = tempMessage;

Try 尝试

var arr = Array.prototype.slice.call(allMessages);
arr.unshift(data);

You can see how does Array.prototype.slice.call() work? 您可以看到Array.prototype.slice.call()如何工作?

Hacky business but for me the following did it: 骇客业务,但对我而言,做到了以下几点:

this.setState({data: this.state.data.reverse().concat(data).reverse()});

So basically change the order put it in last and change the order again, ugly I know but it does work. 因此,基本上改变顺序将其放在最后,然后再次改变顺序,这很丑,但我知道它确实有效。

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

相关问题 如何在JavaScript中取消移位或将其添加到arguments对象的开头 - How to unshift or add to the beginning of arguments object in JavaScript 使用“unshift”方法将项目添加到 Map Object 的开头 - Add item to beginning of an Map Object with "unshift" method 将 object 附加/添加/取消移动到 api 响应的开头 - append/add/unshift an object to the beginning of an response from an api 从对象列表中取消移位而不使用删除? - Unshift from object list without using delete? 如何从对象中的数组中弹出和取消移动元素? - How to pop and unshift elements from an array in object? 从 object 获取值时,unshift() 是否会更改源 object? - When taking values from an object, does unshift() change the source object? javascript:将 object 取消移位到 object 数组 - javascript: unshift an object to an array of object Array.insert函数错误作为Object函数Array(){[native code]}没有方法'insert' - Array.insert function error as Object function Array() { [native code] } has no method 'insert' javascript对象中的推入和移入操作 - Push and Unshift Operation in javascript object 如何在javascript中将对象移至第一个块并将最旧的块移动到新块? - How to unshift object to first chunk and move oldest to new chunk in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM