var data = {
users: [
{
'user': 'barney',
'age': 36,
'active': true,
items:[
{a: 1, b: [{c: 1}, {c:2}]},
{a: 2, b: [{c: 3}, {c:4}]}
]
},
// {
// 'user': 'barney',
// 'age': 36,
// 'active': true,
// items:[
// {a: 2, b: [{c:3}]}
// ]
// },
]
};
const path = 'items/a'; // => 1,2
const path2 = 'items/b/c' // -> 1,2,3,4
function test(p) {
const arr = p.split('/');
const key = arr.pop();
let value = data.users;
while(arr.length > 0) {
value = value.map(item => {
})
}
}
// console.log(_.find(users, function(o) { return o.age < 40; }))
function getLanguageInfoByPath(data, path) {
const results= [];
let key = path;
if (path.includes('.')) {
key = key.split('.')[0];
} else if (path.includes('/')) {
key = key.split('/')[0];
}
if (path.includes('.')) {
results.push({
key: path.replace('.', '_'),
value: _.get(data, path)
});
} else if (path.includes('/')) {
const value = data[key];
const items = path.split('/').filter((item, index) => index > 0);
const last = items.pop();
// console.log(items, last, 1)
value.map((item, index) => {
let temp = _.clone(items);
let array = item;
while(temp.length > 0) {
console.log(temp, 2)
const prop = temp.shift();
array[prop].map((ele) => {
console.log(temp)
if (temp.length ===0) {
console.log(ele, 'ele')
}
array = ele;
})
}
console.log(array, 'array', index)
// results.push({
// key: '1',
// value: item[prop],
// })
})
const targetArray = [];
while (items.length > 0) {
const prop = items.shift();
value.map((item, index) => {
results.push({
key: '1',
value: item[prop],
})
})
}
} else {
results.push({
key,
value: data[key]
});
}
console.log('res', results)
}
// getLanguageInfoByPath(data, 'users/items/b/c')
console