- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Javascript 并创建一个离线应用程序。
我有一个非常大的数组,超过 10mb。我需要保存用户已更改的数组中的元素,但将数组写入/读取到磁盘的速度太慢。
我想创建第二个数组,它只保存用户所做的更改,这样我就可以保存那个较小的数组,然后当我需要加载更改时,它可以将其更改镜像到大数组。
例如:
lib[x][1][y][6] = "no";
libVars[x][1][y][6] = "no";
libVars 将简单地保存 lib 中的元素更改,并且不会与大型数组 lib 的大小相同,因为用户只会与大型数组的一小部分进行交互。
显然这是行不通的,因为 libVars 与 lib 的结构不同,如果这样做也会占用大量内存(无论如何我是这么认为的!)
然后我想遍历 libVars 并在 libVars 中保存更改的相同元素点更新 lib。
有没有办法将某种类型的指针保存到 lib 中的某个位置,我可以将其与关联的元素值一起存储在 libVars 中?
任何帮助将不胜感激。
谢谢。
======================
我的大型阵列示例
var lib = [
[241,[[221119,"sample data","sample","no","no",131,"no"],
[221121,"sample2 data","sample2","no","no",146,"no"],
[221123,"sample3 data","sample3","no","no",28,"no"],
[221626,"sample4 data","sample4","no","no",26,"no"],
[221628,"sample5 data","sample5","no","no",88,"no"]]],
[330,[[305410,"2sample data","sample 2b","no","no",197,"no"],
[305412,"2sample 2 data","sample2 2b","no","no",147,"no"],
[305414,"3sample 2 data","sample3 2b","no","no",10,"no"] ...
尝试 Z-Bone 发布的解决方案
我已经添加了 tempLib var,但我没有看到您最终如何更新实际的 lib 对象
Object.entries(libVars).forEach(([path, value]) => {
var tempLib = lib;
var pathParts = path.split('#');
pathParts.forEach((pathPart, index) => {
if (index == pathParts.length-1) {
tempLib[pathPart] = value;
} else {
//I don't fully track what you are doing here.
//I assume you are building the array path, but to me it looks like
// it's getting wiped out and not built upon?
tempLib = tempLib [pathPart];
}
});
});
最佳答案
2019 年 1 月 28 日更新基于nomaam的测试数据
@nomaam 请检查以下代码段。我让它改变了 lib
数组中的任意路径。我选择的路径是这样的:lib[0][1][3][4]
。您可以看到它最初如何返回值 "no"
,然后在执行修改跟踪解决方案后返回值 "yes"
。
// First Step: Original Data (lib)
var lib = [
[241,[[221119,"sample data","sample","no","no",131,"no"],
[221121,"sample2 data","sample2","no","no",146,"no"],
[221123,"sample3 data","sample3","no","no",28,"no"],
[221626,"sample4 data","sample4","no","no",26,"no"],
[221628,"sample5 data","sample5","no","no",88,"no"]]],
[330,[[305410,"2sample data","sample 2b","no","no",197,"no"],
[305412,"2sample 2 data","sample2 2b","no","no",147,"no"],
[305414,"3sample 2 data","sample3 2b","no","no",10,"no"]]]]
// Log initial value in arbitrary selection lib[0][1][3][4]
console.log(lib[0][1][3][4])
// Second Step: Pointer's object
var libVars = {}
// Example of changing a value via an artificial "pointer" to lib[0][1][3][4]
libVars['0#1#3#4'] = 'yes'; // original value is "no"
// Third Step: Run the modifier - update changes from libVars to lib
Object.entries(libVars).forEach(([path, value]) => {
var tempLib = lib;
var pathParts = path.split('#');
pathParts.forEach((pathPart, index) => {
if (index == pathParts.length - 1) {
tempLib[pathPart] = value;
} else {
tempLib = tempLib[pathPart];
}
});
});
// Log the change fro lib
console.log(lib[0][1][3][4])
原始答案
如果我正确理解了您的挑战,从技术上讲,您可以持有一个对象,该对象将您要修改的属性的路径作为键,将修改后的值作为其值。
假设您的原始对象如下所示:
var lib = {a: {b: { c: { d: "yes"}}}}
您可以在备份对象中保留更改日志(例如,值从"is"更改为“否”)。它可能看起来像这样,用 .
标记嵌套属性。
var libVars = {};
libVars['a.b.c.d'] = "no";
然后当你想更新原始的大数组/值对象时,你可以这样做:
Object.entries(libVars).forEach(([path, value]) => {
var tempLib = lib;
// split by nesting indicator -> dot
var pathParts = path.split('.');
// iterate all parts of the path to the modified value
pathParts.forEach((pathPart, index) => {
if (index == pathParts.length-1) {
// once you made your way to last index, update value
tempLib[pathPart] = value;
} else {
tempLib = tempLib[pathPart];
}
})
})
console.log(lib); // outputs => {a: {b:{c: {d : "no"}}}}
关于Javascript - 链接到更大数组的值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54383908/
我正在开发一个 voip 调用应用程序。我需要做的是在接到来电时将 Activity 带到前台。我在应用程序中使用 Twilio,并在收到推送消息时开始调用。 问题是我试图在接到任何电话时显示 Act
我是一名优秀的程序员,十分优秀!