Hey guys I have an obj that looks a bit like this.
嘿伙计们,我有一个看起来有点像这样的obj。
[
{
"app": 1,
"scalable": true,
"zoomable": true,
"cropBoxResizable": true
},
{
"app": 2,
"scalable": false,
"zoomable": true,
"cropBoxResizable": false
}
]
What I'm trying to is basically extract which ever specific app related object data and save to a second obj. So for example if app = 1 then newObj will be something like this
我正在尝试的是基本上提取哪些特定的应用程序相关的对象数据并保存到第二个obj。因此,例如,如果app = 1,则newObj将是这样的
var newObj = {
"app": 1,
"scalable": true,
"zoomable": true,
"cropBoxResizable": true
}
Is there a way I can make this json format better? I'm open to that too. I'm just trying to figure out the most optimized way to approach this.
有没有办法让这种json格式变得更好?我也对此持开放态度。我只想弄清楚最优化的方法来解决这个问题。
Thank you.
谢谢。
3 个解决方案
#1
2
You need to iterate over your array and check app
to equality
您需要遍历您的数组并检查应用程序是否相等
var arr = [ { "app": 1, "scalable": true, "zoomable": true, "cropBoxResizable": true }, { "app": 2, "scalable": false, "zoomable": true, "cropBoxResizable": false } ]
function extract(a, app) {
var length = a.length;
for(var i = 0; i < length; i++){
if (a[i].app === app) {
return a[i];
}
}
}
var newObj = extract(arr, 2)
console.log(newObj);
Result:
结果:
{ app: 2,
scalable: false,
zoomable: true,
cropBoxResizable: false }
#2
0
try this code , this is the simplest way
试试这段代码,这是最简单的方法
var source = [
{
"app": 1,
"scalable": true,
"zoomable": true,
"cropBoxResizable": true
},
{
"app": 2,
"scalable": false,
"zoomable": true,
"cropBoxResizable": false
}
];
for (var i = 0; i < source.length; i++) {
if (source[i].app == "1") {
console.log(source[i]);
};
};
#3
0
If I understood you well should be much better to use object as a container instead of array.
如果我理解你应该更好地使用对象作为容器而不是数组。
const src = { "1": // this is app name
{
"scalable": true,
"zoomable": true,
"cropBoxResizable": true
},
"2":
{
"scalable": false,
"zoomable": true,
"cropBoxResizable": false
}
};
so then just const appData = src['1'];
那么只需const appData = src ['1'];
#1
2
You need to iterate over your array and check app
to equality
您需要遍历您的数组并检查应用程序是否相等
var arr = [ { "app": 1, "scalable": true, "zoomable": true, "cropBoxResizable": true }, { "app": 2, "scalable": false, "zoomable": true, "cropBoxResizable": false } ]
function extract(a, app) {
var length = a.length;
for(var i = 0; i < length; i++){
if (a[i].app === app) {
return a[i];
}
}
}
var newObj = extract(arr, 2)
console.log(newObj);
Result:
结果:
{ app: 2,
scalable: false,
zoomable: true,
cropBoxResizable: false }
#2
0
try this code , this is the simplest way
试试这段代码,这是最简单的方法
var source = [
{
"app": 1,
"scalable": true,
"zoomable": true,
"cropBoxResizable": true
},
{
"app": 2,
"scalable": false,
"zoomable": true,
"cropBoxResizable": false
}
];
for (var i = 0; i < source.length; i++) {
if (source[i].app == "1") {
console.log(source[i]);
};
};
#3
0
If I understood you well should be much better to use object as a container instead of array.
如果我理解你应该更好地使用对象作为容器而不是数组。
const src = { "1": // this is app name
{
"scalable": true,
"zoomable": true,
"cropBoxResizable": true
},
"2":
{
"scalable": false,
"zoomable": true,
"cropBoxResizable": false
}
};
so then just const appData = src['1'];
那么只需const appData = src ['1'];