[英]nodejs how can i use multidimensional array and push values in it?
i am trying to create a new multidimensional array
from the data i am getting from 3rd part API. 我试图根据我从第三部分API获取的数据创建一个新的
multidimensional array
。
"output":[
{
"is_indb":false,
"name":"adam",
"tokens":29
},
{
"is_indb":true,
"name":"aaron",
"tokens":2,
},
{
"is_indb":false,
"name":"adam",
"tokens":3,
},
{
"is_indb":false,
"name":"axel",
"tokens":5,
},
{
"is_indb":false,
"name":"andy",
"tokens":5,
},
{
"is_indb":false,
"name":"bob",
"tokens":5,
},
{
"is_indb":false,
"name":"aldo",
"tokens":5,
},
{
"is_indb":false,
"name":"julia",
"tokens":5,
}
]
i would like to create a new array
and fill it with data from response. 我想创建一个新的
array
,并用响应中的数据填充它。 but i would like to do some pre checks like 但我想做一些预先检查
take only those whose, is_indb = false
take only those whose, name starts with a
只接受那些
is_indb = false
的对象,只接受其名称以a
开头a
so the final array will be, all those whosse is_indb = true
and name starts with a
所以最终数组将是所有那些
is_indb = true
且名称以a
开头a
var newaray = [[adam,29],[adam,3],[axel,5],[andy,5],[aldo,5]];
so far i have tried using _pluck
and getting some weird outputs. 到目前为止,我已经尝试使用
_pluck
并获得一些奇怪的输出。 i am able to get sible elements using _pluck
but cant get multiple items. 我能够使用
_pluck
获取_pluck
的元素,但无法获取多个项目。
i can think of logic like this 我可以想到这样的逻辑
var newaray = [];
if( (_pluck(msg.output,'is_indb') == false && ((_pluck(msg.output,'name').substring(0, 1) == "a")){
newaray.push( [ _.pluck(msg.output, 'name') , _.pluck(msg.output, 'token')] );
}
Use filter
and map
: 使用
filter
和map
:
var filteredOutput = output
.filter(function(elem) {
// The return statement should return true,
// if you want the element to pass into the new array.
return elem.is_indb === false && typeof elem.name === "string" && elem.name.indexOf('a') === 0;
})
.map(function(elem) {
return [elem.name, elem.tokens];
});
or with ES6: 或使用ES6:
let filteredOutput = output
.filter(elem => elem.is_indb === false && typeof elem.name === "string" && elem.name.indexOf('a') === 0)
.map(elem => [elem.name, elem.tokens])
with ES6 and using regex (inspired by Peter Grainger
's answer, but also case insensitive): 使用ES6并使用正则表达式(受
Peter Grainger
的回答启发,但不区分大小写):
let filteredOutput = output
.filter(elem => elem.is_indb === false && /^a/i.test(elem.name))
.map(elem => [elem.name, elem.tokens])
and by the way, what you posted is an array of objects, not a multidimensional array, which is an array of arrays. 顺便说一句,您发布的是对象数组,而不是多维数组,而多维数组是数组数组。
You could use a filter then a map? 您可以先使用过滤器,再使用地图?
const output = [ { "is_indb":false, "name":"adam", "tokens":29 }, { "is_indb":true, "name":"aaron", "tokens":2, }, { "is_indb":false, "name":"adam", "tokens":3, }, { "is_indb":false, "name":"axel", "tokens":5, }, { "is_indb":false, "name":"andy", "tokens":5, }, { "is_indb":false, "name":"bob", "tokens":5, }, { "is_indb":false, "name":"aldo", "tokens":5, }, { "is_indb":false, "name":"julia", "tokens":5, } ] const transform = output.filter(value => /^a/.test(value.name) && !value.is_indb) .map(value => [value.name, value.tokens]) console.log(transform)
You can use _.filter and get the output in this form 您可以使用_.filter并以这种形式获取输出
op = [{ obj1 } ,{obj2}];
but as you want to remove some keys also then you can use _.pick 但是当您也想删除一些键时,您可以使用_.pick
var op = _.filter(ip , function(obj){
if(obj.is_indb == false && obj.name[0] == 'a'){
return true;
}
else{
return false;
}
})
//so now you have all the entries filtered out
var opAltered = _.pick(op,['name','tokens']);
//you will get this result
/*
opAltered = [
{
name : <something>,
tokens : <something>
},{
...
}
]
*/
or If you want array you can use this 或者,如果您想要数组,可以使用此
opAltered = _.map(op,['name','tokens'];
I have used more code to make you understand properly you can reduce it once you understand Thanks. 我使用了更多的代码来使您正确理解,一旦理解,可以减少代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.