我有一个扁平化数组的问题。
鉴于结构如下
df = data.frame(a = c("Hello", "Hi", "Bye"),
b = c("John", "Jack", "John"),
c = c("Mum", "Dad", "Dad"))
dynaFilter(c("a", "c"), list(c("Hi", "Bye"), c("Dad")))
# returns the same as
# df %>%
# filter(a %in% c("Hi", "Bye"), c %in% c("Dad"))
dynaFilter(c("a", "b", "c"), list(c("Hi", "Bye"), c("Jack") ,c("Dad")))
# returns the same as
# df %>%
# filter(a %in% c("Hi", "Bye"), b %in% c("Jack"), c %in% c("Dad"))
更好地说明
如何将匹配展平为单个对象数组,或者实际上是否具有平面结构,其中所有嵌套项都在单个数组或对象中?
我已经设法通过争论数据来达到目前为止,但似乎坚持这一点我几乎可以使用任何库或工具以及最新的JS功能。
我已尝试映射值,使用lodash deepMerge减少它,但似乎无法实现我想要的效果。
输入:
{
"name": "Somename",
"property": [
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
}
]
]
}
每个匹配道具的预期输出:
const data = [
{
"sport": "Handball",
"matches": [
[
{
"home": "Izmir BSB SK (Youth) (Wom)",
"away": "Bursa Osmangazi (Youth) (Wom)",
"ID": "3092996854"
}
],
[
{
"home": "Al Muheet U21",
"away": "Al Mohmel U21",
"ID": "3092999932"
}
]
]
},
{
"sport": "Volleyball",
"matches": [
[
{
"home": "Ji-Hee Choi/Michika Ozeki",
"away": "Panji Ahmad Maulana",
"ID": "3093062401"
},
{
"home": "Ryazan (Wom)",
"away": "Angara Irkutsk (Wom)",
"ID": "3093062393"
}
],
[
{
"home": "CF Carthage (Wom)",
"away": "Winner - Broughton Excels",
"ID": "3093721823"
}
],
[
{
"home": "Ankara Yildirim Beyazit Universitesi (Wom)",
"away": "Saglik Bilimleri Universitesi (Wom)",
"ID": "3093058567"
}
]
]
}
]
答案 0 :(得分:4)
您可以使用函数reduce
并检查函数Array.isArray
的数组对象。
var data = { "name": "Somename", "property": [ [ { "prop": "someprop", "other": "someother" }, { "prop": "someprop", "other": "someother" } ], [ { "prop": "someprop", "other": "someother" }, { "prop": "someprop", "other": "someother" }, { "prop": "someprop", "other": "someother" }, { "prop": "someprop", "other": "someother" } ], [ { "prop": "someprop", "other": "someother" } ] ]};
data.property = data.property.reduce((mapped, p) =>
[...mapped, ...(Array.isArray(p) ? p : [p])], []);
// ^
// |
// +- This is to check for situations where
// a particular object is not an array.
console.log(JSON.stringify(data, null, 2));
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
使用点差运算符和array.concat
。 array.concat
具有这种独特的条件,如果参数是一个数组,它会将该数组的内容添加到结果数组而不是数组本身。扩展运算符将数组的内容作为函数的args一个接一个地传播。
const flatProperties = [].concat(...data.property)
const data = {
"name": "Somename",
"property": [
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
}
]
]
}
const flatProperties = [].concat(...data.property)
console.log(flatProperties)

答案 2 :(得分:0)
您可以在阵列上使用.reduce
data.property = data.property.reduce(
(acc, el) => acc.concat(el) , []
)
var data = {
"name": "Somename",
"property": [
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
}
]
]
};
data.property = data.property.reduce(
(acc, el) => acc.concat(el) , []
)
console.log(data);
答案 3 :(得分:0)
您可以使用concat
和apply
方法。这个答案类似于 Joseph 但没有ES6数组扩展运算符
var originalArray = {
"name": "Somename",
"property": [
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
}
]
]
};
var flattened = [].concat.apply([],originalArray["property"]);
console.log(flattened)
答案 4 :(得分:0)
如果您使用的是lodash,这也可以。
var data = {
"name": "Somename",
"property": [
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
}
]
]
}
data.property = _.flattenDeep(data.property)
console.log(data);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
&#13;
答案 5 :(得分:0)
您可以使用递归函数遍历数组
const props = {
"name": "Somename",
"property": [
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
},
{
"prop": "someprop",
"other": "someother"
}
],
[
{
"prop": "someprop",
"other": "someother"
}
]
]
};
function smoosh(populate) {
return function _smoosh(arr) {
if ( Array.isArray(arr) ) {
for ( let value of arr ) {
_smoosh(value);
}
}
if ( typeof(arr) === "object" && ! Array.isArray(arr) ) {
populate.push(arr);
}
};
}
const flatten = [];
smoosh(flatten)(props.property);
Object.assign(props,{
property: flatten
});
console.log(props);