我有以下对象数组。我需要获取具有最高import React, { Component } from 'react';
import ReactDOM from "react-dom";
import Pagination from "react-js-pagination";
import {
BrowserRouter as Router,
Route,
IndexRoute,
Link,
} from 'react-router-dom';
import ProjectDetails from './ProjectDetails';
import DashboardContainer from '../UIcomponent/DashboardContainer';
const pannelWidth = {
width: '90%'
};
const pannelHeader = {
color: 'white'
};
const pannelFooter = {
float: 'right'
};
class ProjectList extends Component {
constructor(props) {
super(props);
this.state = {
activePage: 1,
searchText: '',
isdiagram:true,
isMax:false,
projectList: [],
originalProjectList: []
};
//this.handlePageChange = this.handlePageChange.bind(this);
this.projectDetails = this.projectDetails.bind(this);
this.deleteMessage = this.deleteMessage.bind(this);
this.updateInputValue = this.updateInputValue.bind(this);
this.setSize=this.setSize.bind(this);
}
componentDidMount() {
let d = '';
$.get("http://localhost:8008/api/navigation/all", function (data) {
d = data;
this.setState({
projectList: d,
originalProjectList: d
});
}.bind(this));
}
handlePageChange(pageNumber) {
this.setState({ activePage: pageNumber });
console.log(this.state.projectList);
}
projectDetails(item, index) {
console.log(index);
}
deleteMessage(item, index) {
showconfrim("Do you want to delete this Project?", this.deleteProject(item, index));
console.log('delete');
}
deleteProject(item, index) {
$("#confirmwindow").modal('hide');
console.log('delete');
}
setSize(){
this.setState({
isMax:!this.state.isMax
});
if(!this.state.isMax){
//clear style for jquery animate;
$(this.refs.selfdiv).attr("style",null);
setTimeout(()=>{
$(this.refs.selfdiv).animate({
top:'0px',
right: '0px',
bottom: '0px',
left: '0px'
},500);
},100);
}
console.log(this.props.children);
if(this.props.children[1].props['data-event']){
var self=this;
setTimeout(()=>{
self.props.children[1].props['data-event'].call();
},700);
}
}
updateInputValue(event) {
this.setState({
searchText: event.target.value
}, function () {
let textToSearch = this.state.searchText;
let originalData = this.state.projectList;
if (textToSearch != undefined || textToSearch != '') {
let searchData = [];
for (var i = 0; i < this.state.projectList.length; i++) {
if (this.state.projectList[i].name.indexOf(textToSearch) != -1 || this.state.projectList[i].description.indexOf(textToSearch) != -1) {
searchData.push(this.state.projectList[i]);
}
}
this.setState({
projectList: searchData
});
}
if(textToSearch == '') {
this.setState({
projectList: this.state.originalProjectList,
});
}
});
}
render() {
var listItems = this.state.projectList.map((item, index) => {
return <tr key={index}>
<td onClick={e => this.projectDetails(item, index)}><a><u>{item.name}</u></a></td>
<td>{item.description}</td>
<td><i className="glyphicon glyphicon-trash" onClick={e => this.deleteMessage(item, index)}></i></td>
</tr>
});
return (
<div className="container" style={pannelWidth} ref="selfdiv">
<br />
<div className="panel panel-primary">
<div className="panel-heading">
<div className="row">
<div className="col-md-2 col-lg-2">
<h4 style={pannelHeader}>Project List</h4>
</div>
<div className="col-md-6 col-lg-6">
<input type="text" className="form-control" placeholder="Search" value={this.state.searchText} onChange={this.updateInputValue}/>
</div>
<div className="col-md-2 col-lg-2">
<button className="btn btn-sm btn-success">Create New Project</button>
</div>
<div className="col-md-2 col-lg-2">
<div className="captiontoolbar buttoncontainer">
<span onClick={this.setSize} style={pannelFooter} className={
this.state.isMax ? ("boxMaxsize glyphicon glyphicon-resize-small") : ("boxMaxsize glyphicon glyphicon-fullscreen")
}></span>
</div>
</div>
</div>
</div>
<div className="panel-body">
<table className="table table-striped">
<thead>
<tr>
<th><b>Project Name</b></th>
<th><b>Description</b></th>
<th><b>Action</b></th>
</tr>
</thead>
<tbody>
{listItems}
</tbody>
</table>
</div>
<div style={pannelFooter}>
<Pagination
activePage={this.state.activePage}
itemsCountPerPage={3}
totalItemsCount={this.state.projectList.length}
pageRangeDisplayed={5}
onChange={this.handlePageChange.bind(this)}
/>
</div>
</div>
</div>
);
}
}
export default ProjectList;
的项目的属性。我有一个只返回MoveCount
的函数。但我想获得MoveCount
和Latitude
的价值。
我使用的功能如下:
Longitude
物件:
var res = Math.max.apply(Math,movers.map(function(o){
return o.MoveCount, o.Latitude, o.Longitude;}))
alert('Max y = ' + res);
答案 0 :(得分:2)
您可以使用基于MoveCount的自定义排序功能。这将保留整个对象。
var movers = [
{
"MoveCount":3,
"DepartureCountryCode":"MG",
"Latitude":-18.766947,
"Longitude":46.869107
},
{
"MoveCount":2,
"DepartureCountryCode":"MU",
"Latitude":-20.348404,
"Longitude":57.552152
},
{
"MoveCount":4,
"DepartureCountryCode":"ZA",
"Latitude":-30.559482,
"Longitude":22.937506
}
];
movers.sort(function(a,b){ return b.MoveCount - a.MoveCount; });
var highestMoveCount = movers[0]; // get the item with the highest MoveCount
console.log(highestMoveCount);
上面的代码返回一个按MoveCount降序排序的数组。
答案 1 :(得分:1)
对数组进行排序,使用map
函数更改输出的对象并从排序的数组中获取第一项。
var movers = [
{
"MoveCount": 3,
"DepartureCountryCode": "MG",
"Latitude": -18.766947,
"Longitude": 46.869107
},
{
"MoveCount": 2,
"DepartureCountryCode": "MU",
"Latitude": -20.348404,
"Longitude": 57.552152
},
{
"MoveCount": 4,
"DepartureCountryCode": "ZA",
"Latitude": -30.559482,
"Longitude": 22.937506
}];
const highest = movers.sort((a,b) => b.MoveCount - a.MoveCount)
.map(({MoveCount, Latitude, Longitude}) => ({MoveCount, Latitude, Longitude}))[0];
console.log(highest);
&#13;
答案 2 :(得分:1)
你可以减少推动者总是选择更大的推动者:
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 2 MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.action.type = Delete
appender.rolling.strategy.action.basePath = ${sys:es.logs.base_path}${sys:file.separator}
appender.rolling.strategy.action.maxDepth = 1
appender.rolling.strategy.action.ifLastModified.type = IfLastModified
appender.rolling.strategy.action.ifLastModified.age = 3d
答案 3 :(得分:1)
你可以像别人说的那样排序,但这会带来O(nlogn)的复杂性。或者您可以只比较O(n)复杂度中的最高值。
const movers = [{"MoveCount":3,"DepartureCountryCode":"MG","Latitude":-18.766947,"Longitude":46.869107},
{"MoveCount":2,"DepartureCountryCode":"MU","Latitude":-20.348404,"Longitude":57.552152},
{"MoveCount":4,"DepartureCountryCode":"ZA","Latitude":-30.559482,"Longitude":22.937506}]
let highest = movers[0]
for (let index=1; index<movers.length; index += 1) {
let movObj = movers[index];
if (movObj.MoveCount > highest.MoveCount) {
highest = movObj;
}
}
console.log(highest)
// if you only want certain fields
const certainFieldsRes = ({MoveCount, Latitude, Longitude}) => ({
MoveCount, Latitude, Longitude
})
console.log(certainFieldsRes(highest))
答案 4 :(得分:1)
我可能会使用Array.prototype.reduce
来遍历数组,以找到MoveCount
最高的项目。
let movers = [
{
"MoveCount":3,
"DepartureCountryCode":"MG",
"Latitude":-18.766947,
"Longitude":46.869107
},
{
"MoveCount":2,
"DepartureCountryCode":"MU",
"Latitude":-20.348404,
"Longitude":57.552152
},
{
"MoveCount":4,
"DepartureCountryCode":"ZA",
"Latitude":-30.559482,
"Longitude":22.937506
}
];
let getHighestMoveCount = function (arr) {
let o = arr.reduce(function (accumulator, currentValue, currentIndex) {
// if this is the first item, we have nothing to compare against,
// just return this item
if (currentIndex === 0) {
return currentValue;
} else {
// if this item has a higher MoveCount than the current highest,
// return it
if (currentValue.MoveCount > accumulator.MoveCount) {
return currentValue;
} else {
// otherwise return the current highest
return accumulator;
}
}
});
// return the object that has the highest MoveCount
return o;
// This returns the entire object, if you wanted a new object with
// only the MoveCount Latitude and Longitude, you would do
// this instead:
// return {MoveCount: o.MoveCount, Latitude: o.Latitude, Longitude: o.Longitude};
};
console.log(getHighestMoveCount(movers));