我有一个Numbers数组,我正在使用该.push()
方法向其中添加元素.
有没有一种简单的方法从数组中删除特定元素?相当于像array.remove(number);
.
我必须使用核心的JavaScript - 无框架是不允许的.
我正在尝试使用 React 更新对象数组中的单个键值对。
对象数组
const array = [
{id:1,name:'Tyler',age:23},
{id:2,name:'Lauren',age:28},
{id:3,name:'Nico',age:14},
]
Run Code Online (Sandbox Code Playgroud)
给定 id 和年龄,我希望能够更新与 id 匹配的单个对象。例如,给定 id 2 和年龄 56,{id:2,name:'Lauren',age:28}
应更改为{id:2,name:'Lauren',age:56}
我已经设置 useState 来复制对象数组。但是,我正在努力筛选出选定的 id、更改年龄并返回更新后的数组。
这就是我目前所拥有的:
import { useState } from "react";
const array = [
{ id: 1, name: "Tyler", age: 23 },
{ id: 2, name: "Lauren", age: 28 },
{ id: 3, name: "Nico", age: 14 }
];
export default function App() {
const [list, setList] = useState(array);
const updateAge = (id …
Run Code Online (Sandbox Code Playgroud) 我的 removeItem 函数有问题(它应该删除当前<li>
嵌套的按钮,以及 this.state.list 上的数组中的项目),目前没有代码,因为我尝试了很多东西,但没有任何效果,所以我最终console.logs
看的是什么发生了所以我删除了它
import React, { Component } from 'react';
import './Todo.css';
class Todo extends Component {
constructor(props) {
super(props);
this.state = {
list: [],
text: ''
}
this.textChange = this.textChange.bind(this);
this.addToList = this.addToList.bind(this);
this.removeItem = this.removeItem.bind(this);
}
textChange(e) {
this.setState({
text: e.target.value
})
}
addToList() {
this.setState(prevState => ({
list: prevState.list.concat(this.state.text),
text: ''
}))
}
removeItem(e) { ?
? ? ? ? ? ? ?
}
render() {
return(
<div>
<h1>My Todo List</h1> …
Run Code Online (Sandbox Code Playgroud) 我正在为电子商务网站实施购物车。购物车是一个由对象数组表示的状态变量shopCart。每个对象都包含有关产品的信息,例如标题和价格。我正在尝试实现一个删除按钮,它实际上正在执行它的意图,即从shopCart状态中删除项目,但更改未显示在屏幕渲染上。我可以清空购物车,但屏幕仍然显示产品。这是购物车页面的主要代码:
return (
<div class={styles.container}>
<h1>Product</h1><h1>Quantity</h1><h1>Unit price</h1><h1>Total price</h1><div></div>
{
shopCart.map((product, i, array) => <CartItem key={product.id} product={product} index={i} array={array}/>)
}
</div>
)
Run Code Online (Sandbox Code Playgroud)
这是 CartItem.js 的实现
const CartItem = (props) => {
let { shopCart, setShopCart } = useContext(Context);
let product = props.product;
// takes the identification of a shopping cart product and removes it from the cart
const decrease = (element) => {
shopCart.forEach((el, i) => {
if (el.hasOwnProperty('id')) {
if (el.id === element) {
let aux …
Run Code Online (Sandbox Code Playgroud) 我去年和今天发过这篇文章,我认为事情可以简化.
我需要通过索引从数组中删除一个项目.当通过索引时,数组是否具有相同的值并不重要.你的典型例子:
let arr = [1,2,3,2,1] // just an array, not array with objects
let x = 1;
// This will not be an expected result:
// Find all values that is equal to 1 then remove
arr.filter(num => num !== x) //=> [2,3,2]
Run Code Online (Sandbox Code Playgroud)
我的期望是当我删除最后一个元素(1
)时,例如,数组应该是[1,2,3,2]
:
let index = 4; // which is the last "1" in the array
let indexVal = arr.indexOf(4) // 1
let newArray = arr.splice(indexVal, 1) //=> [1,2,3,2]
Run Code Online (Sandbox Code Playgroud)
现在,它是2017年,差不多'18,是否有更短的方式(es5/6)这样做没有任何polyfil? …
我遇到一个问题,尽管状态已成功更新,但通过传入数组更改状态后 React 不会呈现:
const [phoneNumber, setPhoneNumber] = useState([])
...
const newNumber = phoneNumber
newNumber.pop()
setPhoneNumber(newNumber)
Run Code Online (Sandbox Code Playgroud)
但是,在使用扩展运算符传递 newNumber 后,应用程序按预期重新呈现:
const newNumber = phoneNumber
newNumber.pop()
setPhoneNumber([...newNumber])
Run Code Online (Sandbox Code Playgroud)
这是什么原因造成的?