我是javascript新手。你能帮我从这个数组做吗:
[ [ '00011111', 'ADMIN' ],
[ '00033333', 'ACTIVITY' ],
[ '00022222', 'SUPPORT' ],
[ '00011111', 'MEMBER' ],
[ '00022222', 'MEMBER' ],
[ '00033333', 'MEMBER' ] ]
这个:
[ [ '00011111', 'ADMIN', 'MEMBER' ],
[ '00033333', 'ACTIVITY', 'MEMBER' ],
[ '00022222', 'SUPPORT', 'MEMBER' ] ]
先谢谢您。 如果您可以建议几个变体,那就太好了:)
答案 0 :(得分:1)
您可以通过检查公用键是否在结果数组中来缩小数组,然后将其推入值或将新数组推入结果集。
var array = [['00011111', 'ADMIN'], ['00033333', 'ACTIVITY'], ['00022222', 'SUPPORT'], ['00011111', 'MEMBER'], ['00022222', 'MEMBER'], ['00033333', 'MEMBER']],
result = array.reduce((r, [k, v]) => {
var temp = r.find(([key]) => k === key);
if (temp) {
temp.push(v);
} else {
r.push([k, v]);
}
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:1)
注意:具有for循环的简单策略
@Bean
public OAuth2RestOperations sampleROPCRestTemplate() {
return new OAuth2RestTemplate(sampleforcePasswordResourceDetails(), new DefaultOAuth2ClientContext(new DefaultAccessTokenRequest()));
}
@Bean
protected OAuth2ProtectedResourceDetails sampleforcePasswordResourceDetails() {
ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
resource.setAccessTokenUri(tokenUrl);
resource.setClientId(clientId);
resource.setClientSecret(clientSecret);
resource.setUsername(username);
resource.setPassword(password);
resource.setClientAuthenticationScheme(AuthenticationScheme.form);
resource.setGrantType("password");
return resource;
}
答案 2 :(得分:1)
使用ID和列表时,我建议使用一个对象。
通过这种方式,我们可以将id用作索引,这使得处理数据更加容易:
//Original data
var originalData = [
['00011111', 'ADMIN'],
['00033333', 'ACTIVITY'],
['00022222', 'SUPPORT'],
['00011111', 'MEMBER'],
['00022222', 'MEMBER'],
['00033333', 'MEMBER']
];
//The new variable, which is in the form of an object
var newData = {};
//Loop through original data
originalData.forEach(function(dataPoints) {
//If first time we encounter this id, create an array on newdata using the first element as key
if (newData[dataPoints[0]] === void 0) {
newData[dataPoints[0]] = [];
}
//Push secondary elements to our object, using the first element as key
for (var index = 1; index < dataPoints.length; index++) {
var dataPoint = dataPoints[index];
newData[dataPoints[0]].push(dataPoint);
}
});
//Output our new data object
console.log(newData);
//get data by id
console.log(newData["00033333"]);
答案 3 :(得分:0)
route.post('/pay',async(req,res)=>{
try {
const link = 'https://app.sandbox.midtrans.com/snap/v1/transactions'
const obj = {"transaction_details": {"order_id":"order-12","gross_amount": 5000}}
const data = await axios.post(link,obj,
{
headers : {
"Accept": "application/json",
"Content-Type": "application/json",
Authorization: `Basic ${Base64.encode('*******')}`
}
})
const koin = data.data.token
transaction.create({
order_id : obj.transaction_details.order_id,
gross_amount : obj.transaction_details.gross_amount,
token : koin
})
console.log(data.data.token)
res.json(data)
} catch (error) {
console.log(error)
}
})
此方法首先将原始列表缩小为以下形式的对象:
const input = [ [ '00011111', 'ADMIN' ],
[ '00033333', 'ACTIVITY' ],
[ '00022222', 'SUPPORT' ],
[ '00011111', 'MEMBER' ],
[ '00022222', 'MEMBER' ],
[ '00033333', 'MEMBER' ] ]
const expected = [ [ '00011111', 'ADMIN', 'MEMBER' ],
[ '00033333', 'ACTIVITY', 'MEMBER' ],
[ '00022222', 'SUPPORT', 'MEMBER' ] ]
const collate = xs => Object.entries(xs.reduce(
(a, [id, type]) => ({
...a,
[id]: [...(a[id] || []), type ]
})
, {})).map(
([id, types]) => [id, ...types]
)
console.log(collate(input))
然后遍历该对象条目,将每个条目折叠为一个平面数组。