将 javascript 对象从一种形式转换为另一种形式
Transform javascript object from one form to another
输入
const products = {
100: ['abc', 'xyz', 'mno', 'pqr'],
200: ['mno', 'pqr'],
300: ['abc', 'xyz'],
400: ['abc', 'pqr'],
}
预计
{
abc: [100, 300, 400],
xyz: [100, 300],
mno: [100, 200],
pqr: [100, 400]
}
我的解决方案:
var results = {};
Object.keys(products).forEach(function(id){
console.log(id)
products[id].forEach(function(user){
if(typeof results[user] === 'undefined'){
results[user] = [];
}
results[user].push(id)
})
})
console.log(results)
有没有更好的方法,比如 map、reduce..等?
这里是 Array Reduce
const products=
{ 100: [ 'abc', 'xyz', 'mno', 'pqr']
, 200: [ 'mno', 'pqr']
, 300: [ 'abc', 'xyz']
, 400: [ 'abc', 'pqr']
}
var result= Object.keys(products).reduce((acc, elm)=>
{
for (let ref of products[elm])
{
if (!acc[ref]) acc[ref]= [Number(elm)]
else acc[ref].push(Number(elm))
}
return acc
}
,{})
for (trace in result) console.log(trace, ':', JSON.stringify(result[trace]))
与 @Bergi 在他的评论中提出的双重减少相同:
const products=
{ 100: [ 'abc', 'xyz', 'mno', 'pqr']
, 200: [ 'mno', 'pqr']
, 300: [ 'abc', 'xyz']
, 400: [ 'abc', 'pqr']
}
var result = Object.keys(products).reduce((acc, elm) =>
products[elm].reduce((acc, ref) =>
{
if (!acc[ref]) acc[ref]= [Number(elm)]
else acc[ref].push(Number(elm))
return acc
}
, acc)
, {})
for (trace in result) console.log(trace, ':', JSON.stringify(result[trace]))
你的方法很好,我会根据你的要求再提供一个map/reduce例子。
const products = {
100: ['abc', 'xyz', 'mno', 'pqr'],
200: ['mno', 'pqr'],
300: ['abc', 'xyz'],
400: ['abc', 'pqr'],
}
let result = Object.entries(products)
.flatMap(([id,tokens])=>tokens.map(token=>({token,id})))
.reduce((index,{token,id})=>{
if(index[token]) index[token].push(id)
else index[token] = [id]
return index
},{})
console.log(result)
输入
const products = {
100: ['abc', 'xyz', 'mno', 'pqr'],
200: ['mno', 'pqr'],
300: ['abc', 'xyz'],
400: ['abc', 'pqr'],
}
预计
{
abc: [100, 300, 400],
xyz: [100, 300],
mno: [100, 200],
pqr: [100, 400]
}
我的解决方案:
var results = {};
Object.keys(products).forEach(function(id){
console.log(id)
products[id].forEach(function(user){
if(typeof results[user] === 'undefined'){
results[user] = [];
}
results[user].push(id)
})
})
console.log(results)
有没有更好的方法,比如 map、reduce..等?
这里是 Array Reduce
const products=
{ 100: [ 'abc', 'xyz', 'mno', 'pqr']
, 200: [ 'mno', 'pqr']
, 300: [ 'abc', 'xyz']
, 400: [ 'abc', 'pqr']
}
var result= Object.keys(products).reduce((acc, elm)=>
{
for (let ref of products[elm])
{
if (!acc[ref]) acc[ref]= [Number(elm)]
else acc[ref].push(Number(elm))
}
return acc
}
,{})
for (trace in result) console.log(trace, ':', JSON.stringify(result[trace]))
与 @Bergi 在他的评论中提出的双重减少相同:
const products=
{ 100: [ 'abc', 'xyz', 'mno', 'pqr']
, 200: [ 'mno', 'pqr']
, 300: [ 'abc', 'xyz']
, 400: [ 'abc', 'pqr']
}
var result = Object.keys(products).reduce((acc, elm) =>
products[elm].reduce((acc, ref) =>
{
if (!acc[ref]) acc[ref]= [Number(elm)]
else acc[ref].push(Number(elm))
return acc
}
, acc)
, {})
for (trace in result) console.log(trace, ':', JSON.stringify(result[trace]))
你的方法很好,我会根据你的要求再提供一个map/reduce例子。
const products = {
100: ['abc', 'xyz', 'mno', 'pqr'],
200: ['mno', 'pqr'],
300: ['abc', 'xyz'],
400: ['abc', 'pqr'],
}
let result = Object.entries(products)
.flatMap(([id,tokens])=>tokens.map(token=>({token,id})))
.reduce((index,{token,id})=>{
if(index[token]) index[token].push(id)
else index[token] = [id]
return index
},{})
console.log(result)