想要检查对象数组中所有行的值是否相同,或者对象数组中所有列的值是否相同。我如何使用lodash有效地做到这一点?
String keyStorePassword = "123456";
String configPath = "C:\\ssl\\";
String keyStorePath = configPath + "client.ks";
KeyStore ks = KeyStore.getInstance("jks");
String trustStore = configPath + "trust.ts";
java.lang.System.setProperty("javax.net.ssl.trustStore", trustStore);
java.lang.System.setProperty("javax.net.ssl.trustStorePassword", keyStorePassword);
InputStream ksIs = new FileInputStream(keyStorePath);
try {
ks.load(ksIs, keyStorePassword.toCharArray());
} finally {
if (ksIs != null) {
ksIs.close();
}
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks, keyStorePassword.toCharArray());
TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
}
};
final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
ConnectionFactory factory = new ActiveMQSslConnectionFactory(URL);
sslContext.init(kmf.getKeyManagers(), trustAllCerts, new SecureRandom());
SslContext context = new SslContext();
context.setSSLContext(sslContext);
SslContext.setCurrentSslContext(context);
Connection connection = factory.createConnection(loginName, pwd);
connection.start();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer nonPersistentProducer = session.createProducer(null);
session.close();
connection.close();
在上面的例子中,对象数组中的所有行都是相同的。
答案 0 :(得分:0)
如果我理解正确的约束,那怎么样?
var data = [
{row: 0, col: 4},
{row: 0, col: 1},
{row: 0, col: 2},
];
function allTheSame(data, key) {
return data.length && _.every(data, function (item) {
return item[key] === data[0][key];
});
}
console.log(_.any(['row', 'col'], function (key) {
return allTheSame(data, key);
}));
请注意_.every
和_.any
都提前纾困,所以这应该尽可能高效。
另请注意,如果数组中没有元素,allTheSame
将返回false
。如果您希望将空列表视为“完全相同”,则可以切换到return data.length === 0 || _.every(...
。
答案 1 :(得分:0)
棘手的部分是在与lodash的同一次迭代中尝试检查列值或行值是否相同。但是,除非您正在处理无法形容的数据,否则这不应该是必要的。假设情况并非如此,这是简单的线性方法:
function isRowOrColSame(data) {
var row = _.every(data, {'row': data[0].row})
var col = _.every(data, {'col': data[0].col})
return (row || col)
}
var data = [
{row: 0, col: 4},
{row: 0, col: 1},
{row: 0, col: 2}
]
console.log(isRowOrColSame(data))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>