var obj = {
name: "Simon",
age: "20",
clothing: {
style: "simple",
hipster: false
}
}
for(var propt in obj){
console.log(propt + ': ' + obj[propt]);
}Run Code Online (Sandbox Code Playgroud)
变量如何propt表示对象的属性?它不是内置方法或属性.为什么它会出现在对象中的每个属性?
好吧,这更像是一个计算机科学问题,而不是一个基于特定语言的问题,但是地图操作和foreach操作之间有区别吗?或者它们只是同一个东西的不同名称?
我在map和foreach中看到的唯一区别map是返回一个数组而forEach不是.但是,我甚至不理解方法的最后一行forEach" func.call(scope, this[i], i, this);".例如,是不是" this"和" scope"指的是同一个对象而不是this[i]并且i指的是循环中的当前值?
我在另一篇文章中注意到有人说" forEach当你想根据列表的每个元素做某事时使用.例如,你可能会在页面上添加内容.基本上,它非常适合你想要的"副作用".我不知道副作用是什么意思.
Array.prototype.map = function(fnc) {
var a = new Array(this.length);
for (var i = 0; i < this.length; i++) {
a[i] = fnc(this[i]);
}
return a;
}
Array.prototype.forEach = function(func, scope) {
scope = scope || this;
for (var i = 0, l = this.length; i < l; i++) {
func.call(scope, this[i], i, this);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,这些方法在javascript中是否有任何实际用途(因为我们不更新数据库),除了操纵这样的数字:
alert([1,2,3,4].map(function(x){ …Run Code Online (Sandbox Code Playgroud) 如果我有一个网址数组:
var urls = ['1.txt', '2.txt', '3.txt']; // these text files contain "one", "two", "three", respectively.
Run Code Online (Sandbox Code Playgroud)
我想构建一个如下所示的对象:
var text = ['one', 'two', 'three'];
Run Code Online (Sandbox Code Playgroud)
我一直在努力学习如何做到这一点fetch,这当然会回归Promises.
有些事情我已经试过了不工作:
var promises = urls.map(url => fetch(url));
var texts = [];
Promise.all(promises)
.then(results => {
results.forEach(result => result.text()).then(t => texts.push(t))
})
Run Code Online (Sandbox Code Playgroud)
这看起来不对,无论如何它都不起作用 - 我最终没有数组['one','two','three'].
Promise.all在这里使用正确的方法?
我想删除目录中的几个文件,匹配正则表达式.像这样的东西:
// WARNING: not real code
require('fs').unlink(/script\.\d+\.js$/);
Run Code Online (Sandbox Code Playgroud)
由于unlink不支持正则表达式,我使用它代替:
var fs = require('fs');
fs.readdir('.', (error, files) => {
if (error) throw error;
files.filter(name => /script\.\d+\.js$/.test(name)).forEach(fs.unlink);
});
Run Code Online (Sandbox Code Playgroud)
这是有效的,但IMO比它应该更复杂.
是否有更好的内置方法来删除与正则表达式匹配的文件(甚至只是使用通配符)?
我正在学习JavaScript。我写了这段代码来学习map函数。但是后来我感到困惑,为什么为什么不连续映射它,因为每个映射序列都会将一个新元素推入数组。它不应该在映射过程中继续推动新元素吗?为什么map函数仅对原始的三个元素运行,而对新的推入元素不运行?
我试图在节点环境中对其进行调试,并且该arr变量处于关闭状态。我知道闭包是什么,但我无法理解这里发生了什么。
let array = [1, 2, 3];
array.map((element) => {
array.push(10);
console.log(element);
});Run Code Online (Sandbox Code Playgroud)
我希望输出应该是 1,2,3,10,10,10,10,10,10,10,10......10
但是实际输出仅为1,2,3。
假设我有一个列表,我想使用映射函数向其中添加一些值:
const arr = [1, 2, 3, 4, 5];
const anotherArr = [];
Run Code Online (Sandbox Code Playgroud)
我使用函数式方法来做到这一点:
arr.map((item) => anotherArr.push(item));
Run Code Online (Sandbox Code Playgroud)
这是一个反模式/错误的逻辑——也就是说,不使用映射操作返回值做任何事情?这方面有什么好的资源吗?
(我知道这个逻辑很愚蠢,我可以复制列表 - 这不是我的问题的重点)
这可能是一个 2 班轮,但由于某种原因,我碰壁了。
我想将对象数组转换为键值对对象。
所以这:
var items = [
{
name: 'hello',
value: ['one', 'two']
},
{
name: 'hi',
value: ['one', 'two', 'three']
}
]
Run Code Online (Sandbox Code Playgroud)
对此:
var items = {
'hello': ['one', 'two'],
'hi': ['one', 'two', 'three']
}
Run Code Online (Sandbox Code Playgroud)
这真的是最优雅的方式吗?
const newObj = {};
items.forEach((item) => {
newObj[item.name] = item.value;
});
Run Code Online (Sandbox Code Playgroud)
我想最好使用 ES6 箭头函数。另外,有人可以告诉我您是否认为以第一种或第二种格式操作这些数据会更容易吗?对于上下文,我正在尝试自学拓扑排序。
我正在尝试创建一个表React组件,它将数据和标题作为道具.我遇到了一个问题,我无法得到forEachMap 的方法,不会创建必要的子元素.
列的输入prop如下所示:
let cols = {
payment: 'Payment',
date: 'Processing Date',
amount: 'Amount',
payee: 'Payee'
};
Run Code Online (Sandbox Code Playgroud)
这是我的React表组件的一个方法,它生成标题元素:
generateTableHead() {
let columnsMap = new Map((Object.entries(this.props.columns)));
let i = 0;
return (
<tr>
{columnsMap.forEach((columnValue) =>
<th key={i++}>
{columnValue}
</th>
)}
</tr>
);
}
Run Code Online (Sandbox Code Playgroud)
问题是<th>孩子不存在于被<tr>返回的对象中; 我得到了一个emtpy <tr></tr>.
但是,如果我使用Array.prototype.map,则<th>可以正确创建子元素WILl.
generateTableHead() {
let columnsArray = Object.entries(this.props.columns);
return (
<tr>
{columnsArray.map((column, index) =>
<th key={index}>{column[1]}</th>
)}
</tr>
);
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢第一个版本,因为代码稍微有点理解,因为我将在Map对象中引用键和值,而不是在第二个版本中使用数组索引.
我确信这有一个非常简单的解释,或者我做了一个非常简单的错误,我无法发现.如果有人能指出问题,我将非常感激!
去年,我更频繁地使用数组方法,如映射和过滤器,而不是数组上的标准 for 循环。它感觉更容易阅读和编写,并且可以完成我最有可能要做的所有事情,例如创建局部变量。
但很多时候我不会返回任何东西。不过埃斯林特不太喜欢我。据他们说,他们说你总是需要返回,否则“可能是一个错误” https://eslint.org/docs/rules/array-callback-return
为什么?只是好的做法吗?无返回数组方法有哪些缺点?
考虑这个问题有一段时间了。任何见解或想法都会很棒。
我有一个位置数组,试图在页面上显示其值,我使用了以下代码来遍历该数组:
{this.props.locations && this.props.locations.forEach(loc => {
console.log("Location: "+loc)
return(
<span>Location is: {loc}</span>
)
})} Run Code Online (Sandbox Code Playgroud)
页面上的结果为空: 在此处输入图像描述, 但是它正确记录了位置: 在此处输入图像描述
我在App.js中获得this.props.locations的值,如下所示:
var locations = this.state.cardLists
var distictLocations = new Set()
locations.forEach(location => {
if(!distictLocations.has(location.Location))
{
distictLocations.add(location.Location)
}
});
this.setState({
locations: distictLocations
})Run Code Online (Sandbox Code Playgroud)
我不确定自己在做什么错。任何帮助,将不胜感激。
大家好,我有一个这样的数组:
[
{
"name": "test",
"amount": 794.651786,
"id": "60477897fd230655b337a1e6"
},
{
"name": "test2",
"amount": 10.80918,
"id": "60477bfbfd230655b337a1e9"
}
]
Run Code Online (Sandbox Code Playgroud)
我不想计算每笔金额的总和。
我尝试使用这样的 useState 挂钩:
const [total, setTotal] = useState(Number);
array.map((item) => {
setTotal(total + item.amount);
});
Run Code Online (Sandbox Code Playgroud)
但它似乎没有按预期工作。
javascript ×10
arrays ×4
ecmascript-6 ×3
foreach ×3
map-function ×3
reactjs ×3
array-push ×1
closures ×1
dictionary ×1
es6-promise ×1
eslint ×1
fetch-api ×1
file-io ×1
filesystems ×1
key-value ×1
loops ×1
node.js ×1
object ×1
promise ×1
prototype ×1
react-hooks ×1
react-jsx ×1